Why is there ambiguity in this diamond pattern?

Posted by cambr on Stack Overflow See other posts from Stack Overflow or by cambr
Published on 2010-04-17T13:48:44Z Indexed on 2010/04/17 14:03 UTC
Read the original article Hit count: 438

Filed under:
|
#include <iostream>
using namespace std;

class A             { public: void eat(){ cout<<"A";} };
class B: public A   { public: void eat(){ cout<<"B";} };
class C: public A   { public: void eat(){ cout<<"C";} };
class D: public B,C { public: void eat(){ cout<<"D";} };

int main(){
    A *a = new D();
    a->eat();
}

I am not sure this is called diamond problem or not, but why doesn't this work?

When I said, a->eat() (remember eat() is not virtual), there is only one possible eat() to call, that of A.

Why then, do I get this error:

'A' is an ambiguous base of 'D'

© Stack Overflow or respective owner

Related posts about c++

Related posts about diamond-problem