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
If what you request would be allowed it would mean that a call to blah() via the base class must do a conversion from Inner2 to Inner... a cast is not possible since the caller is responsible for managing the returned object (since it is not returned by pointer/reference but by value) and will reserve space for it on the stack. So it can only handle Inner not Inner2 or whatever ancestor class.
So you then have an instance of Inner, not Inner2... So you don't get any advantage...
Why is that?
Simply because the ISO C++ standard committee ruled it that way!
There is no fundamental reason. It could be done differently, at the cost of a tiny additional compiler complexity.
objects declared on the runtime stack must be known to the compiler at compile time:
void f(const A & a) {
Inner inr = a.blah( );
}
inr must be of both static and dynamic type "Inner" (not "Inner2") to be allocated off the stack -- so if blah returned an Inner2 it would be used to construct an Inner and it's "2ness" would be lost (or sliced as K Dragon mentions).
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.
The blah
method have to return A::Inner in all cases -- if you think of the reason it is quite simple. A instance of B could easily be case to the base class A. Now if anybody calls blah on A (the base class which object should it return? Inner2 or Inner?