Force all classes to implement / override a 'pure virtual' method in multi-level inheritance hierarchy

前端 未结 5 900
孤城傲影
孤城傲影 2020-12-01 20:10

In C++ why the pure virtual method mandates its compulsory overriding only to its immediate children (for object creation), but not to

5条回答
  •  [愿得一人]
    2020-12-01 21:05

    In your example, you have not declared D::foo pure; that is why it does not need to be overridden. If you want to require that it be overridden again, then declare it pure.

    If you want to be able to instantiate D, but force any further derived classes to override foo, then you can't. However, you could derive yet another class from D that redeclares it pure, and then classes derived from that must override it again.

提交回复
热议问题