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

前端 未结 5 908
孤城傲影
孤城傲影 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 20:42

    Is there any practical way of having this effect in C++?

    No, and for good reason. Imagine maintenance in a large project if this were part of the standard. Some base class or intermediate base class needs to add some public interface, an abstract interface. Now, every single child and grandchild thereof would need to changed and recompiled (even if it were as simple as adding using D::foo() as you suggested), you probably see where this is heading, hells kitchen.

    If you really want to enforce implementation you can force implementation of some other pure virtual in the child class(s). This can also be done using the CRTP pattern as well.

提交回复
热议问题