10

I have some troubles with the application of polymorphism in this example. This question is similar to my last question

C++, virtual inheritance, strange abstract class + clone problem

There are 3 abstract classes:

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

A::~A(){}

class B
{
public:
    virtual B  * copy () const = 0;
    virtual ~B() = 0;
};

B::~B(){}

class C: virtual public A , public B 
{
public:
    virtual C  * copy () const = 0;
    virtual ~C() = 0;
};

C::~C(){}

and two inherited classes using the virtual inheritance

class D: virtual public A
{
public:
    virtual D  * copy () const {return new D  (*this);}
    virtual ~D() {}
};

class E: virtual public D , public C
{
public:
    virtual E * copy () const {return new E (*this);}
    virtual ~E() {}
}; //Error C2250: 'E' : ambiguous inheritance of 'D *A::copy(void) const

The above mentioned error occurs only using MSVS 2010 compiler, g++ compiles this code OK.

Class diagram (simplified)

.......... A .... B.....
........../.\..../......
........./...\../.......
......../.....\/........
.......D...... C........
........\...../.........
.........\.../..........
..........\./...........
...........E............

Last discussion we close with the result: remove the declaration of the copy() method from class C.

class C: virtual public A , public B 
{
public:
    //virtual C  * copy () const = 0; //remove declaration
    virtual ~C() = 0;
};

C::~C(){}

My sample code using polymorphism needs to create vector of pointers to C. After removing some element I want to create its copy... I NEED a declaration of copy() in class C, so removal of the declaration is insufficient and it does not solve the problem.

int main(int argc, char* argv[])
{
std::vector <C*> items;
items.push_back(new E());
items.push_back(new E());
items[0]->copy();
return 0;
}

Could you help me, please how to correct the code to be translatable using VS 2010?

Community
  • 1
  • 1
Johnas
  • 1,263
  • 3
  • 12
  • 23
  • 1
    Look up `virtual` inheritance in your favorite C++ book, FAQ, or reference site. Also search the web for "C++ dreaded diamond inheritance". – Thomas Matthews Aug 03 '11 at 20:50
  • I know this problem... But how does it how relate to my question? This code is correct, but MSVS 2010 does not compile it however g++ ant oher compilers yes... – Johnas Aug 03 '11 at 21:03

1 Answers1

10

This is a known bug in Visual C++:

Visual C++ incorrectly reports ambiguity when covariance is used with virtual inheritance

You either need to eliminate the covariance or the virtual inheritance. Unfortunately, you can't have both.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Thanks for your answer. If I understand correctly, so the error will corrected in the next a release VS... Is there any other way how to compile the code using MSVS2010 (compiler settings...)? – Johnas Aug 03 '11 at 21:00
  • 2
    No, the bug was closed as "Won't Fix" meaning it won't be fixed in the next release. It appears that it is not possible to use Visual C++ to compile code that combines virtual inheritance with covariance. If this is an important issue for you, please go to that Connect bug, upvote it, click the "I can too" button under "N users can repro this bug," and leave a comment. – James McNellis Aug 03 '11 at 21:03
  • It's annoying. Could we expect that this problem will ever be sometimes fixed? I do not have any experience with bug reporting to Microsoft... – Johnas Aug 03 '11 at 21:26