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.
Only pointers and references can be covariant.