强制派生类在非抽象基类中重载虚拟方法 [英] Forcing a derived class to overload a virtual method in a non-abstract base class
问题描述
我正在使用简单的克隆模式编写一些代码,如果我能够强制派生类重写该克隆模式,但又保留了使用我的基类的能力,我会很喜欢。 (因此,我不想将clone方法声明为纯虚拟方法。)
I'm writing some code using a simple clone pattern, I'd like it if I were able to force derived classes to override that clone pattern, but retain the ability to use my base class. (So I don't want to declare the clone method to be pure virtual.)
是否有在编译器级别强制实施此限制的方法?
Is there anyway to enforce this restriction at the compiler level?
推荐答案
不幸的是,在C ++中没有办法实现这一目标。您不能强制在子类中重写非抽象方法。但是,我可能会注意到,具体的基类在C ++中应该很少见,您可能需要重新考虑设计。有了有关您总体目标的更多信息,我们也许可以为您的确切需求提供更好的答案。
Unfortunately there is just no way to make this happen in C++. You can't force a non-abstract method to be overridden in child classes. However, I might note that concrete base classes should be fairly rare in C++ and you might want to reconsider your design. With more information about your overall aims we might be able to provide a better answer for your precise needs.
这篇关于强制派生类在非抽象基类中重载虚拟方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!