C++ Forcing Method Override In Concrete Class

后端 未结 5 2454

Is there a way in C++ to write a concrete class which when another class is derived from it, has a method which must be overriden. An abstract class allows the forcing of the de

5条回答
  •  一向
    一向 (楼主)
    2021-02-20 06:48

    One option is to put all the implementation of the class into an abstract superclass, and inherit from that instead.

    • Move all the implementation of your concrete class T into an abstract class S.
    • In S, make the must-be-overridden method a pure virtual function -- and provide a definition of it.
    • T subclasses S.
    • In T, override the must-be-overridden function, and call the S implementation.
    • Instead of subclassing T, subclass S.
    • Prevent subclassing of T.

提交回复
热议问题