Using subclass type parameters in virtual functions

后端 未结 4 1314
甜味超标
甜味超标 2021-02-05 13:15

I have this piece of code (contrived from my real-life trouble)

It cannot compile, complaining ExtendsB does not implement B::Run(A* a). However, it has no

4条回答
  •  無奈伤痛
    2021-02-05 13:33

    Specialising the return type makes the subclass more strict - it will act as an A. However, restricting the Run method to accept only a subclass of the original argument makes B not act as an A.

    Inheritance models an is-a relationship. Your code violates the Liskov Substitution principle.

提交回复
热议问题