如何强制所有子级覆盖父级的 Clone() 方法?
如何确保所有派生的 C++/CLI 类都将重写基类的 ICloneable::Clone() 方法?
你认为我应该担心这个吗? 或者说这不是基类作者的责任?
修正:抱歉,我忘了提及基类是非抽象类。
How to make sure that all derived C++/CLI classes will override the ICloneable::Clone() method of the base class?
Do you think I should worry about this? Or this is not a responsibility of the base class' writer?
Amendment: Sorry, I forgot to mention that the base class is a non-abstract class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
阅读赫伯·萨特的此。 这正是你要问的
read this by herb sutter. It's exactly what you are asking
托马斯是正确的,但使该类抽象的一种方法是定义一个纯虚方法。
这是通过以下语句完成的:
virtual void Clone() = 0;
除非派生类实现 Clone,否则他们将无法实例化它,因此如果他们希望自己的类有用,他们将别无选择。
Thomas is correct but one way you would make that class abstract is to define a pure virtual method.
This is done by saying:
virtual void Clone() = 0;
Unless the derived class implements Clone they won't be able to instantiate it so they'll have little choice if they want their class to be useful.
从这个新角度来看,我很确定您根本不想强迫任何人覆盖 Clone() 。 例如,如果我的派生类没有添加任何字段,它可能不需要自己专门的 Clone() 方法。
In this new light, I'm pretty sure that you do not want to force anyone to override Clone() at all. For example, if my derived class does not add any fields it probably does not need its own specialized Clone() method.
经过一番思考,我找到了这个解决方案:
如果您尝试克隆未重写基类的 Clone() 方法的派生类的实例,它会抛出 NotImplementedException 。
After some pondering I found this solution:
It throws NotImplementedException if you attempt to Clone an instance of a derived class that hasn't overridden the Clone() method of the base class.
是正确的。
如果您想要克隆的一些默认行为,请尝试:
is correct.
If you want some default behaviour for Clone, try:
如果基类是非抽象的,则无法在编译时强制覆盖它。 您可以做的最好的事情可能是:
这样,派生类必须重写该方法,否则您的应用程序将遇到 NotSupportedException。 这至少会在测试过程中立即明显地看出某些内容是不正确的。 它会给你一些东西来寻找,这样你就知道什么时候遇到一个没有正确覆盖 Clone 的类。 根据您对派生类的控制程度,这对于鲁棒性可能很重要。
If the base class is non-abstract, then there is no way to force it to be overridden at compile time. The best you can probably do is something like:
With this, derived classes would have to override the method or your application will encounter a NotSupportedException. This at least would make it immediately obvious during testing that something was incorrect. It would give you something to look for so that you know when you encounter a class that did not correctly override Clone. Depending on how much control you have over derived classes, this could be important for robustness.
将 Clone() 方法声明为抽象方法。 即使父类确实有具体的实现,这也应该起作用。
当然,强制执行此类操作的风险是派生类的编写者会变得恼火,说“无论如何我都不会使用克隆”并执行诸如字节复制之类的操作,甚至“返回此”,以摆脱错误。
Declare the Clone() method as abstract. This should work even when the parent class does have a concrete implementation.
Of course, the risk when enforcing such things is that the writer of the derived class will become annoyed, say "I'm not going to use Clone anyway" and does something like a bytewise copy, or even a "return this", to get rid of the errors.
好吧,我不能说这是否是基类的责任,并且不会在这里陷入基于继承的契约的危险。
无论如何,您可以强制某些类重写方法 - 例如“Clone()”,通过使其成为抽象类的纯虚拟成员,
请注意“摘要”和“=0;”。 抽象允许类在没有警告的情况下包含纯虚拟成员,并且 =0; 意味着这个方法是纯虚拟的 - 也就是说,它不包含主体。 请注意,您不能实例化抽象类。
现在您可以
如果您在 ClonableChild 中没有克隆覆盖,您会收到编译器错误。
Well, I can't say if this is the responsibility of the base class or not, and won't get into the perils of inheritance based contracts here.
In any case, you can force some class to override a method - "Clone()" for example, by making it a pure virtual member of an abstract class
note the "abstract" and the "=0;". The abstract allows the class to contain pure virtual members without warning, and the =0; means that this method is pure virtual - that is, it doesn't contain a body. Note that you can not instantiate an abstract class.
Now you can
If you do NOT have the Clone override in ClonableChild, you get a compiler error.
在基类中将其声明为纯虚函数。
class Base
{
...
虚拟 void Clone() = 0;
};
Declare it pure virtual in the base class.
class Base
{
...
vitual void Clone() = 0;
};