Ah, SO came back just in time.
I am getting a strange error:
'B::blah': overriding virtual function return type differs and is not covariant from 'A::blah'
Here is the code causing the problem:
class A {
public:
class Inner { };
virtual Inner blah() = 0;
};
class B : public A {
public:
class Inner2 : public Inner { };
Inner2 blah() {
return Inner2();
}
};
I looked up the error, and according to a page I found on the Microsoft website, one of the ways types can be covariant is if:
the class in the return type of B::f is the same class as the class in the return type of D::f or, is an unambiguous direct or indirect base class of the class in the return type of D::f and is accessible in D
Is that not the case with Inner
and Inner2
? I am using Microsoft Visual C++ 2010 if it matters.
OK, so thanks to John I learned that only pointers and references can be covariant. Why is that? A Derived can be cast to a Base, so why don't virtual functions with return types that are derived from the same thing just cast the return type to the one of the base class? In my example, it seems like it would make sense to have (A*(new B))->blah()
return a Inner
that is really an Inner2
that has been cast up.
Imagine that your example worked.
Consider the following code:
A *a = some_function();
A::Inner inner = a->blah();
If the dynamic type of a
is B*, then a->blah()
calls a::B->blah()
, and returns a B::Inner
. This is then silently sliced into an instance of A::Inner
. In general, this (and any type of slicing) is not what you want. I find this to be a good restriction.
Only pointers and references can be covariant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With