[英]How to use clone() in C++ with multiple inheritance of abstract classes?
I am working on a C++ program, but I am having problem with multiple inheritance when using cloning. 我正在开发一个C ++程序,但是在使用克隆时我遇到了多重继承的问题。 The problem (in a simplified form) is the following.
问题(以简化形式)如下。
I want to be able to clone all objects derived from the class Base. 我希望能够克隆从Base类派生的所有对象。
class Base{
public:
virtual Base* clone()const=0;
};
I want to define two other classes derived from Base, which are both abstract, ie I cannot define the clone function, but I have to declare them in some way (I want to make sure, that if I clone Derived*, I will get back Derived* and not Base*, ie I want to avoid casting at the point of application) 我想定义另外两个派生自Base的类,它们都是抽象的,即我无法定义克隆函数,但我必须以某种方式声明它们(我想确保,如果我克隆Derived *,我会得到返回派生*而不是基础*,即我想避免在申请时施放)
class Derived1: public virtual Base{
public:
virtual Derived1* clone()const=0;
};
class Derived2: public virtual Base{
public:
virtual Derived2* clone()const=0;
};
The problem comes, when I declare a fourth class, which is inherited from both Derived1 and Derived2: 当我声明第四个类时,问题就来了,它继承自Derived1和Derived2:
class Derived3: public Derived1,public Derived2{
protected:
int b;
public:
Derived3():b(3){};
Derived3(Derived3 const& l_B) {b=l_B.b;};
virtual Derived3* clone()const{return new Derived3(*this);}
;
};
In this case I will get from Visual C++ 2010 compiler C2250: 'Derived3' : ambiguous inheritance of 'Derived1 *Base::clone(void) const'. 在这种情况下,我将从Visual C ++ 2010编译器C2250获得:'Derived3':'Derived1 * Base :: clone(void)const'的模糊继承。 If I declare clone() in Derived1 and Derived2 not pure virtual, but without definition, the error remains the same:
如果我在Derived1和Derived2中声明clone()不是纯虚拟的,但没有定义,则错误保持不变:
class Base{
public:
virtual Base* clone()const=0;
};
class Derived1: public virtual Base{
public:
virtual Derived1* clone()const;
};
class Derived2: public virtual Base{
public:
virtual Derived2* clone()const;
};
class Derived3: public Derived1,public Derived2{
protected:
int b;
public:
Derived3():b(3){};
Derived3(Derived3 const& l_B) {b=l_B.b;};
virtual Derived3* clone()const{return new Derived3(*this);}
;
};
Using virtual inheritance at Derived3 will not help either, and I cannot find a way to solve it, I simply run out of ideas. 在Derived3上使用虚拟继承也无济于事,我找不到解决它的方法,我只是用尽了想法。 It is very important that all classes should return a pointer of the same type, eg I want to do later:
所有类都应该返回相同类型的指针是非常重要的,例如我想稍后做:
Derived3 test;
Derived1* test2=&test;
Derived1* test3=test2->clone();
and I want to avoid: 我想避免:
Derived3 test;
Derived1* test2=&test;
Derived1* test3=dynamic_cast<Derived1*>(test2->clone());
If anyone has an idea or solution, I would appreciate it! 如果有人有想法或解决方案,我将不胜感激!
The idea would be to have a protected virtual method clone_impl
in Base
and a public non-virtual clone
: 我们的想法是在
Base
使用受保护的虚拟方法clone_impl
和公共非虚拟clone
:
class Base
{
protected:
virtual Base* clone_impl() const = 0;
public:
Base* clone() const
{
return clone_impl();
}
};
and for each derived class provide clone_impl
when the class is not abstract and a non-virtual wrapper clone
for all derived classes: 并且对于每个派生的类提供
clone_impl
当类不是抽象和非虚拟包装clone
对所有派生类:
class DerivedX : ...
{
protected:
// only when not abstract:
virtual Base* clone_impl() const
{
return new DerivedX(*this);
}
public:
// in each derived class
DerivedX* clone() const
{
return static_cast<DerivedX*>(clone_impl());
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.