C++奇怪的钻石继承问题
我有这个
A
/ \
B C
\ /
D
A 有一个纯虚函数,原型为:
virtual A* clone(void) const = 0;
B 和 C 实际上继承自 A ( class B: public virtual A
, class C: public virtual A
)
B具有虚函数,原型为:
virtual B* clone(void) const {};
C 具有虚函数,原型为:
virtual C* clone(void) const {};
D 继承自 B 和 B 。 C 像这样:class D: public B, public C
D 有虚函数,原型为:
virtual D* clone(void) const {};
现在,编译时出现以下 6 行错误:
error C2250: 'D' : ambiguous inheritance of 'B *A::clone(void) const'
不知道如何解决这个问题。
提前致谢。
I have this
A
/ \
B C
\ /
D
A has a pure virtual function, prototyped as:
virtual A* clone(void) const = 0;
B and C virtually inherit from A ( class B: public virtual A
, class C: public virtual A
)
B has the virtual function, prototyped as:
virtual B* clone(void) const {};
C has the virtual function, prototyped as:
virtual C* clone(void) const {};
D inherits from both B & C like that: class D: public B, public C
D has the virtual function, prototyped as:
virtual D* clone(void) const {};
Now, when compiling I get the following 6 lines of errors:
error C2250: 'D' : ambiguous inheritance of 'B *A::clone(void) const'
No freaking idea how to solve this issue.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只需要层次结构中父级的一份副本,请使用虚拟继承。
编辑:
MSVC++ 2010 中可能存在错误。Intellisense 未检测到问题,但编译器会卡住。奇怪的是 VC6 对此很满意。
作为解决方法,如果您按如下方式声明 D,它会使 MSVC++ 2010 满意,同时也可以在没有此问题的编译器中工作:
Use virtual inheritance if you want only one copy of a parent in your hierarchy.
Edit:
There may be a bug in MSVC++ 2010. The Intellisense doesn't detect a problem, but the compiler chokes on it. Strange since VC6 is happy enough with it.
As a workaround, if you declare D as follows, it makes MSVC++ 2010 happy while also working in compilers without this issue:
您在原始帖子中描述的内容完全合法。一个快速示例代码,完全由 Comeau Online 编译器进行编译,没有任何错误
。要么您没有按照您所说的操作,要么您的编译器已损坏。发布您尝试编译的真实代码。
PS 我刚刚尝试在 VS 2010 Express 中编译它并得到相同的错误。正如 Gunslinger47 在评论中指出的那样,这是 VS 2010 编译器中的一个错误。
What you describe in your original post is perfectly legal. A quick sample code that does exactly that compiles without any errors by Comeau Online compiler
Either you are not doing what you said you are doing, or your compiler is broken. Post real code you are trying to compile.
P.S. I just tried compiling this in VS 2010 Express and got the same error. As Gunslinger47 also suggests in the comments, this is a bug in VS 2010 compiler.
避免钻石继承? ;->
无论如何,这里是示例(真正的示例 - 不要那样转换)
// ConsoleCppTest.cpp :定义控制台应用程序的入口点。
//
avoid diamond inheritance? ;->
anyway, here is sample (really sample - don't cast like that)
// ConsoleCppTest.cpp : Defines the entry point for the console application.
//