如何在具有抽象类的多重继承的C ++中使用clone()? [英] How to use clone() in C++ with multiple inheritance of abstract classes?
问题描述
我正在使用C ++程序,但是使用克隆时遇到多重继承问题.问题(以简化形式)如下.
I am working on a C++ program, but I am having problem with multiple inheritance when using cloning. The problem (in a simplified form) is the following.
我希望能够克隆所有从Base类派生的对象.
I want to be able to clone all objects derived from the class Base.
class Base{
public:
virtual Base* clone()const=0;
};
我想定义另外两个从Base派生的类,它们都是抽象的,即我无法定义克隆函数,但是我必须以某种方式声明它们(我想确保,如果我克隆Derived *,我将返回Derived *而不是Base *,即我想避免在应用程序时进行强制转换
I want to define two other classes derived from Base, which are both abstract, i.e. 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*, i.e. I want to avoid casting at the point of application)
class Derived1: public virtual Base{
public:
virtual Derived1* clone()const=0;
};
class Derived2: public virtual Base{
public:
virtual Derived2* clone()const=0;
};
问题来了,当我声明第四个类时,该类同时继承自Derived1和Derived2:
The problem comes, when I declare a fourth class, which is inherited from both Derived1 and 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);}
;
};
在这种情况下,我将从Visual C ++ 2010编译器C2250中获得:'Derived3':'Derived1 * Base :: clone(void)const'的不明确继承.如果我在Derived1和Derived2中声明clone()不是纯虚拟的,但是没有定义,则错误仍然相同:
In this case I will get from Visual C++ 2010 compiler C2250: 'Derived3' : ambiguous inheritance of 'Derived1 *Base::clone(void) const'. If I declare clone() in Derived1 and Derived2 not pure virtual, but without definition, the error remains the same:
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);}
;
};
在Derived3上使用虚拟继承也无济于事,而且我找不到解决的方法,我简直是无济于事.所有类都应返回相同类型的指针,例如我想稍后再做:
Using virtual inheritance at Derived3 will not help either, and I cannot find a way to solve it, I simply run out of ideas. It is very important that all classes should return a pointer of the same type, e.g. I want to do later:
Derived3 test;
Derived1* test2=&test;
Derived1* test3=test2->clone();
我想避免:
Derived3 test;
Derived1* test2=&test;
Derived1* test3=dynamic_cast<Derived1*>(test2->clone());
如果有人有想法或解决方案,我将不胜感激!
If anyone has an idea or solution, I would appreciate it!
推荐答案
该想法是在Base
中具有受保护的虚拟方法clone_impl
,并具有公共非虚拟clone
:
The idea would be to have a protected virtual method clone_impl
in Base
and a public non-virtual clone
:
class Base
{
protected:
virtual Base* clone_impl() const = 0;
public:
Base* clone() const
{
return clone_impl();
}
};
当 not 抽象类和 all 的非虚拟包装器clone
时,
和每个派生类提供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:
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());
}
};
这篇关于如何在具有抽象类的多重继承的C ++中使用clone()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!