Q<B>
and Q<A>
are unrelated classes. Imagine you are a client of B
calling test()
: what do you assign the result to, if you do not know what type it is going to have?
The fact that both Q<A>
and Q<B>
are instances of the same class template does not change the fact that they are two completely unrelated classes, possibly with a completely different layout (due to template specialization).
This would not be any different from doing:
struct X
{
virtual std::string test() = 0;
};
struct Y : X
{
virtual int test() { return 42; } // ERROR! std::string and int are
// unrelated, just as Q<A> and Q<B>
};
The client calling test()
on a pointer to X
would expect the result to be a string
, but "Whoops!", the object pointed to by that pointer is of type Y
, and the return type of Y::test()
is int
. What should happen? A run-time crash?
Y y;
X* p = &y;
std::string s = p->test(); // D'OH!
C++ is a statically typed language, meaning that type checking is performed at compile-time. In this case, the message from the compiler is there to tell you that the derived class does not adhere to the interface of the class it derives from.
If you are wondering what "invalid covariant return type" means, and in particular the word "covariant", that's easily explained.
Suppose you have a base class B
with a virtual function foo()
that returns an X*
:
struct B
{
virtual X* foo();
};
And suppose that you have a class D
derived from B
that overrides foo()
by returning an Y*
, where Y
is a class derived from X
:
struct D : B
{
virtual Y* foo();
};
Is this a problem? Well, the right answer comes from answering this slightly better question: "Would that be a problem for a client calling foo()
that expects an X*
to be returned?"
And the answer to that question is obviously "No", since Y
is a derived class of X
, so you can return a pointer to Y
instead of a pointer to X
:
D d;
B* b = &d;
X* p = b->foo(); // Returns an Y*, but that's OK, because a pointer to Y can be
// assigned to a pointer to X
This is an example of a covariant return type. In your example, the return type of C::test()
is not covariant with respect to the return type of B::test()
.