Diamond problem in C++
- by Jack
I know the diamond problem. I am using gcc compiler. I have some scenarios I need explanation about.
1)
class A{ public: virtual void eat(){cout<<"A eat\n";} };
class B:public A{ public: void eat(){ cout<<"B eat\n";}};
class C:public A{ public: void eat(){ cout<<"C eat\n";}};
class D:public B,C{ public: void eat(){ cout<<"D eat\n";}};
int main()
{
A * a = new D();
a->eat();
getch();
return 0;
}
Why doesn't this work?
2)
class A{ public: void eat(){cout<<"A eat\n";} };
class B:virtual public A{ public: void eat(){ cout<<"B eat\n";}};
class C:virtual public A{ public: void eat(){ cout<<"C eat\n";}};
class D: public B,C{ public: void eat(){ cout<<"D eat\n";}};
int main()
{
A * a = new D();
a->eat();
getch();
return 0;
}
When I do this what happens in the background. How does the ambiguity get removed. Is the concept of vtables involved here?