How does virtual inheritance solve the diamond problem?
Posted
by cambr
on Stack Overflow
See other posts from Stack Overflow
or by cambr
Published on 2010-04-17T16:32:54Z
Indexed on
2010/04/17
16:43 UTC
Read the original article
Hit count: 512
class A { public: void eat(){ cout<<"A";} };
class B: virtual public A { public: void eat(){ cout<<"B";} };
class C: virtual 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 understand the diamond problem, and above piece of code does not have that problem.
How exatly does virtual inheritance solve the problem?
What I understand:
When I say A *a = new D();, the compiler wants to know if an object of type D can be assigned to a pointer of type A, but it has two paths that it can follow, but cannot decide by itself.
So, how does virtual inheritance resolve the issue (help compiler take the decision)?
© Stack Overflow or respective owner