overriding virtual function return type differs and is not covariant

后端 未结 6 1695
后悔当初
后悔当初 2020-12-17 23:36

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          


        
相关标签:
6条回答
  • 2020-12-17 23:44

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

    0 讨论(0)
  • 2020-12-17 23:47

    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.

    0 讨论(0)
  • 2020-12-17 23:49

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

    0 讨论(0)
  • 2020-12-17 23:51

    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.

    0 讨论(0)
  • 2020-12-18 00:03

    Only pointers and references can be covariant.

    0 讨论(0)
  • 2020-12-18 00:03

    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?

    0 讨论(0)
提交回复
热议问题