Inhertance Diamond problem
-
Hi, I have a very famous problem with multiple inheritance here. class A{ public: A(){ cout<<"A"<
laksh2204 wrote:
I am not able to understand that why the constructor of A is called again after B. Or why not the same is happening after constructor of C is called.
So the compiler goes likes this... First time it sees "new D" it 1. calls constructor of D 2. which in turn calls constructor of B (Since B if the first base class) 3. which in turn calls constructor of A (Prints "A") 4. then (Prints "B") 5. then the compiler calls constructor of C (Second base class) 6. which in turn again calls constructor of A (Prints "A") 7. then (Prints "C") 8. in the end (Prints "D") So bottom line is unless base class constructor code executes derived class constructor doesn't execute.
Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
modified on Wednesday, August 27, 2008 8:58 AM
-
Hi, I have a very famous problem with multiple inheritance here. class A{ public: A(){ cout<<"A"<
This is due to the fact that the base class constructor has to get called before the derived class constructor gets call. since D derives from both B and C constructor of A gets called in either of the cases
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
laksh2204 wrote:
I am not able to understand that why the constructor of A is called again after B. Or why not the same is happening after constructor of C is called.
So the compiler goes likes this... First time it sees "new D" it 1. calls constructor of D 2. which in turn calls constructor of B (Since B if the first base class) 3. which in turn calls constructor of A (Prints "A") 4. then (Prints "B") 5. then the compiler calls constructor of C (Second base class) 6. which in turn again calls constructor of A (Prints "A") 7. then (Prints "C") 8. in the end (Prints "D") So bottom line is unless base class constructor code executes derived class constructor doesn't execute.
Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
modified on Wednesday, August 27, 2008 8:58 AM
OK, thats nice explanation.. one more issue comes into my mind. A / \ B C \ / D In the same diamond structure if my declaration is like: class A; class B : virtual public A; class C : virtual public A; class D : public B, public C; now when i do A *d = new D; //it will work as ambiguity is removed by virtual. But, which path will be followed?? A->B->D or A->C->D?? or something else...
-
OK, thats nice explanation.. one more issue comes into my mind. A / \ B C \ / D In the same diamond structure if my declaration is like: class A; class B : virtual public A; class C : virtual public A; class D : public B, public C; now when i do A *d = new D; //it will work as ambiguity is removed by virtual. But, which path will be followed?? A->B->D or A->C->D?? or something else...
laksh2204 wrote:
A *d = new D; //it will work as ambiguity is removed by virtual. But, which path will be followed?? A->B->D or A->C->D??
Now your output will be A B C D I guess first base class gets the preference.
Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
-
Hi, I have a very famous problem with multiple inheritance here. class A{ public: A(){ cout<<"A"<
In Addition to Nibu's comment, it's better to approach diamond inheritance in terms of Virtual Inheritance. Diamond Problem[^] Virtual Inheritance [^] Virtual Inheritance- C++ FAQ[^]
-Sarath. "Great hopes make everything great possible" - Benjamin Franklin
My blog - Sharing My Thoughts, An Article - Understanding Statepattern