如何克隆多重继承对象?
我定义了一个 Cloneable 接口:
struct Cloneable
{
virtual Cloneable * clone(void) const = 0;
}
我还有一些其他接口类(与问题无关的内容):
struct Interface
{
};
struct Useful_Goodies
{
};
我创建了一个继承自上述类的叶对象:
struct Leaf : public Cloneable, public Interface, public Useful_Goodies
{
Leaf * clone(void) const // Line #1 for discussion.
{
return new Leaf(*this);
}
};
我收到错误:
overriding virtual function return type differs and is not covariant from 'Cloneable::clone'
如果我将类型更改为 Cloneable *
,我收到此错误消息:
'return' : ambiguous conversions from 'Leaf *' to 'Cloneable *'
我的问题(所有相关):
- 叶类如何解析
Cloneable
的要求 界面? - 有没有更好的解决方案来实现 克隆合约,其中所有对象 能保证实现克隆吗?
我使用这个范例作为通用编程(记录、字段和数据库)的一部分。
编译器:MS Visual Studio 2008;平台:Windows XP 和 Windows XP远景
I have defined a Cloneable interface:
struct Cloneable
{
virtual Cloneable * clone(void) const = 0;
}
I have also some other interface classes (content not relevant to issue):
struct Interface
{
};
struct Useful_Goodies
{
};
I have created a leaf object which inherits from the above classes:
struct Leaf : public Cloneable, public Interface, public Useful_Goodies
{
Leaf * clone(void) const // Line #1 for discussion.
{
return new Leaf(*this);
}
};
I'm getting the error:
overriding virtual function return type differs and is not covariant from 'Cloneable::clone'
If I change the type to Cloneable *
, I get this error message:
'return' : ambiguous conversions from 'Leaf *' to 'Cloneable *'
My Questions (all related):
- How can the leaf class resolve the
requirements of theCloneable
interface? - Is there a better solution to implement a
Cloning contract, where all objects
are guaranteed to implement cloning?
I'm using this paradigm as part of generic programming (records, fields & database).
Compiler: MS Visual Studio 2008; Platforms: Windows XP & Vista
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让您的
clone
函数返回Cloneable *
是正确的。如果您的接口之一也派生自
Cloneable
,您将得到不明确的转换。编辑: Alf 在评论中指出,
Leaf::clone
不仅可以返回Leaf*
,而且实际上更适合它这样做。我纠正了。Having your
clone
function return aCloneable *
is correct.You will get an ambiguous conversion if one of your interfaces also derives from
Cloneable
.Edit: Alf points out in the comments that not only is it possible for
Leaf::clone
to return aLeaf*
, it's actually preferable for it to do so. I stand corrected.您可能没有提到
Interface
或其他一些基类也继承了Cloneable
。 “不明确的转换”意味着Leaf
可能包含多个Cloneable
基类子对象。 (协变返回类型的问题可能是同一问题的直接结果。)您需要使用 虚拟继承(推荐和链接阅读:C++ FAQ Lite 主题 25.8 到 25.13)。首先,将
: public Cloneable
的所有实例更改为: public virtual Cloneable
。You probably failed to mention that
Interface
or some other base class also inheritsCloneable
. The "ambiguous conversion" meansLeaf
probably contains multipleCloneable
base class subobjects. (A problem with covariant return type could be a direct result of the same problem.)You'll want to solve this problem using virtual inheritance (recommended and linked reading: C++ FAQ Lite topics 25.8 through 25.13). To start with, change all instances of
: public Cloneable
to: public virtual Cloneable
.我可以冒险说,您可能实际上没有从多个路径继承
Cloneable
。也就是说,除了直接的Cloneable
之外,您的一些其他基础(直接或间接)继承自Cloneable
。这使得从Leaf*
到Cloneable*
的转换变得不明确,因为Leaf
中有多个Cloneable
基数。简单的解决方案是使用接口的虚拟继承:
虚拟继承意味着即使
Interface
和Test
都继承自Cloneable
,也只有一个单个Cloneable
基础对象。I can risk and say that you are probably non virtually inheriting from
Cloneable
from more than one path. That is, some of your other base besides the directCloneable
inherits (directly or indirectly) fromCloneable
. This makes the conversion fromLeaf*
toCloneable*
ambiguous as there are more than oneCloneable
base in yourLeaf
.The simple solution is using virtual inheritance from the interface:
Virtual inheritance means that even if both
Interface
andTest
inherit fromCloneable
, there is only a singleCloneable
base object.