overriding virtual function return type differs and is not covariant

后端 未结 6 1708
后悔当初
后悔当初 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: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).

提交回复
热议问题