Here's a Clone()
implementation for my class:
这是我的类的Clone()实现:
MyClass^ Clone(){
return gcnew MyClass(this->member1, this->member2);
}
Now I have about 10 classes derived from MyClass
. The implementation is the same in each case. Owing to the fact that I need to call gcnew
with the actual class name in each case, I am required to create 10 nearly identical implementations of Clone()
.
现在我有大约10个派生自MyClass的类。每种情况下的实施都是相同的。由于我需要在每种情况下使用实际的类名称调用gcnew,因此我需要创建10个几乎完全相同的Clone()实现。
Is there a way to write one single Clone()
method in the base class which will serve all 10 derived classes?
有没有办法在基类中编写一个单独的Clone()方法,它将为所有10个派生类提供服务?
Edit: Is there a way to invoke the constructor of a class via one of it's objects? In a way that will invoke the actual derived class constructor. Something like:
编辑:有没有办法通过其中一个对象调用类的构造函数?以某种方式调用实际的派生类构造函数。就像是:
MyClass ^obj2 = obj1->Class->Construct(arg1, arg2);
I'm doing this on C++/CLI but answers from other languages are welcome.
我在C ++ / CLI上这样做,但欢迎来自其他语言的答案。
In plain old C++, you can do this with compile-time polymorphism (the curiously-recurring template pattern). Assuming your derived classes are copyable, you can just write:
在普通的旧C ++中,您可以使用编译时多态性(奇怪的重复模板模式)来实现这一点。假设您的派生类是可复制的,您可以写:
class Base
{
public:
virtual Base* Clone() const = 0;
//etc.
};
template <typename Derived>
class BaseHelper: public Base
{
//other base code here
//This is a covariant return type, allowed in standard C++
Derived * Clone() const
{
return new Derived(static_cast<Derived *>(*this));
}
};
Then use it like:
然后使用它像:
class MyClass: public BaseHelper<MyClass>
{
//MyClass automatically gets a Clone method with the right signature
};
Note that you can't derive from a class again and have it work seamlessly - you have to "design in" the option to derive again by templating the intermediate classes, or start re-writing Clone
again.
请注意,您无法再次从类派生并使其无缝工作 - 您必须“设计”通过模板化中间类再次派生的选项,或者再次开始重新编写克隆。
Not in C++ that I'm aware of. As you say, you need to create an object of a different class in each implementation of Clone().
不是我所知道的C ++。正如您所说,您需要在Clone()的每个实现中创建不同类的对象。
Hm, I think you can use Factory pattern here. I.e.:
嗯,我想你可以在这里使用工厂模式。即:
MyClass Clone(){
return MyClassFactory.createInstance(this.getClass(), this.member1, this.member2, ...);
}
In the factory, you would have to create instance of subclass based on passed class type. So probably it has the same disadvantages as your approach.
在工厂中,您必须根据传递的类类型创建子类的实例。所以它可能与你的方法有相同的缺点。
I would suggest using copy constructors instead (as derived classes can call the base implementation's copy constructor as well) -- also handy, as it will be familiar territory for C++ programmers.
我建议使用复制构造函数(因为派生类也可以调用基本实现的复制构造函数) - 也很方便,因为它将成为C ++程序员熟悉的领域。
You might be able to create a single Clone method that uses reflection to call the copy constructor on itself in this instance.
您可以创建一个单独的Clone方法,该方法使用反射在此实例中调用自身的复制构造函数。
Possibly also worth noting that Jeffrey Richter said in the Framework Design Guidelines book, "The ICloneable interface is an example of a very simple abstraction with a contract that was never explicitly documented. Some types implement this interface's Clone
method so that it performs a shallow copy of the object, whereas some implementations perform a deep copy. Because what this interface's Clone
method should do was never fully documented, when using an object with a type that implements ICloneable
, you never know what you're going to get. This makes the interface useless" (emphasis mine)
可能还值得注意的是Jeffrey Richter在框架设计指南书中说:“ICloneable接口是一个非常简单的抽象示例,其合同从未明确记录。有些类型实现了此接口的Clone方法,因此它执行浅复制对象,而某些实现执行深度复制。因为这个接口的Clone方法应该做的事情从未完全记录,当使用具有实现ICloneable的类型的对象时,你永远不知道你将得到什么。这使得界面无用“(强调我的)
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2009/05/19/8efd6e4ca25ac38653827e4148977395.html。