简体   繁体   English

C ++奇怪的钻石继承问题

[英]C++ Weird Diamond inheritance issue

I have this 我有这个

    A
  /   \
 B     C
  \   /
    D

A has a pure virtual function, prototyped as: A具有纯虚函数,原型如下:

virtual A* clone(void) const = 0;

B and C virtually inherit from A ( class B: public virtual A , class C: public virtual A ) B和C实际上从A继承( class B: public virtual Aclass C: public virtual A

B has the virtual function, prototyped as: B具有虚函数,原型为:

virtual B* clone(void) const {}; 

C has the virtual function, prototyped as: C具有虚函数,原型为:

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: D继承了B&C,如: class D: public B, public C D具有虚函数,原型为:

virtual D* clone(void) const {};

Now, when compiling I get the following 6 lines of errors: 现在,在编译时我得到以下6行错误:

error C2250: 'D' : ambiguous inheritance of 'B *A::clone(void) const'

No freaking idea how to solve this issue. 毫无疑问如何解决这个问题。

Thanks in advance. 提前致谢。

Use virtual inheritance if you want only one copy of a parent in your hierarchy. 如果只需要层次结构中父项的一个副本,请使用虚拟继承。

class B : public virtual A

Edit: 编辑:
There may be a bug in MSVC++ 2010. The Intellisense doesn't detect a problem, but the compiler chokes on it. MSVC ++ 2010中可能存在错误.Intellisense没有检测到问题,但编译器会对其进行扼流。 Strange since VC6 is happy enough with it. 奇怪,因为VC6很开心。

As a workaround, if you declare D as follows, it makes MSVC++ 2010 happy while also working in compilers without this issue: 作为一种解决方法,如果您按如下方式声明D,它会使MSVC ++ 2010感到高兴,同时也可以在没有此问题的编译器中工作:

class D: public virtual A, public B, public C

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 一个快速的示例代码,完全可以编译而没有Comeau Online编译器的任何错误

class A {
public: virtual A* clone() const = 0;
};

class B: public virtual A {
public: virtual B* clone() const { return 0; }
};

class C: public virtual A {
public: virtual C* clone() const { return 0; }
};

class D: public B, public C
{
public: virtual D* clone() const { return 0; }
};

Either you are not doing what you said you are doing, or your compiler is broken. 要么你没有做你说你正在做的事情,要么你的编译器坏了。 Post real code you are trying to compile. 发布您尝试编译的真实代码。

PS I just tried compiling this in VS 2010 Express and got the same error. PS我刚试过在VS 2010 Express中编译它并得到了同样的错误。 As Gunslinger47 also suggests in the comments, this is a bug in VS 2010 compiler. 正如Gunslinger47在评论中所暗示的那样,这是VS 2010编译器中的一个错误。

avoid diamond inheritance? 避免钻石继承? ;-> ; - >

anyway, here is sample (really sample - don't cast like that) 无论如何,这里是样本(真的是样本 - 不要像那样投射)

// ConsoleCppTest.cpp : Defines the entry point for the console application. // ConsoleCppTest.cpp:定义控制台应用程序的入口点。 // //

#include "stdafx.h"
#include "iostream"

class A {
public:
    virtual void* clone() = 0;
};

class B: public A {
public:
    virtual void* clone() = 0;
};

class C: public A {
    public:
    virtual void* clone() = 0;
};

class D: public B, public C
{
public:


    virtual void* B::clone() 
    {
        std::cout << "B";
        return (void*)this;
    }

    virtual void* C::clone()
    {
        std::cout << "C";
        return (void*)this;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{

    D* d = new D();

    void* b = ((B*)d)->clone();

    void* c = ((C*)d)->clone();

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM