overriding virtual function return type differs and is not covariant

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

提交回复
热议问题