The following C++ code gives me these errors when compiled:
covariant.cpp:32:22: error: invalid covariant return type for ‘virtual Q<B> C::test()’
covariant.cpp:22:22: error: overriding ‘virtual Q<A> B::test()’
I do not want to change the line virtual Q<B> test() {}
to virtual Q<A> test() {}
although it removes the compilation errors. Is there another way to solve this problem?
template <class T>
class Q
{
public:
Q() {}
virtual ~Q() {}
};
class A
{
public:
A() {}
virtual ~A() {}
};
class B
{
public:
B() {}
virtual ~B() {}
virtual Q<A> test() = 0;
};
class C : public B
{
public:
C() {}
virtual ~C() {}
virtual Q<B> test() {}
};
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()
.
The function with the signature B::test(void)
returns an object of type Q<A>
, while C::test(void)
(which is the same signature, so you're overwriting the function) returns Q<B>
. I think that is impossible.
As far as I know it is impossible to overload a function by return type and overwrites of parent functions need to stick to the same return type.
From the Standard §10.3/7
The return type of an overriding function shall be either identical to the return type of the overridden function or covariant with the classes of the functions. If a function D::f overrides a function B::f, the return types of the functions are covariant if they satisfy the following criteria:
- both are pointers to classes, both are lvalue references to classes, or both are rvalue references to
classes112
- 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 and accessible direct or indirect base class of the class in the return type of D::f
- both pointers or references have the same cv-qualification and the class type in the return type of D::f has the same cv-qualification as or less cv-qualification than the class type in the return type of B::f.
You cannot do that. Overrides of virtual functions cannot change the prototype of the function, except very specific cases, such as the covariant return types.
Covariant return would be valid if you were to return in the virtual override a subclass of the type returned in the virtual base. But your Q<A>
and Q<B>
are unrelated by inheritance. The fact that B
is a subclass of A
does not make any difference here.