overriding virtual function return type differs and is not covariant

时光毁灭记忆、已成空白 提交于 2019-11-29 06:48:36

Only pointers and references can be covariant.

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...

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.

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).

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!