C++ Forcing Method Override In Concrete Class

后端 未结 5 2451

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:41

    As others have mentioned you can "fix" this for one level of derivation, by splitting your current concrete class into an abstract class and a concrete class.

    The not uncommon generalization of that approach is to have a class template that must be used to create a concrete class out of each abstract class. For example, it could supply a clone method. Or, in Microsoft's ATL, it supplies IUnknown interface implementation.

    However, the abstract class hierarchy with auto-generated concrete leafs is in general too much work and complication. The practical approach is to instead just document what has to be overridden, like clone. You can't in general force other's code to be correct, you can only help it along in that direction.

    By the way, if you could be more concrete about your particular problem, then there could be far better answers than this general wishwash.

    However, by generalizing your problem, you generalized the answers.

    Cheers & hth.,

提交回复
热议问题