#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    18
    Rep Power
    0

    Smile c++ multi inheritance simple question


    Hi Guys..
    I'm looking at the following code:

    Code:
    class A{
    public:
    	int a;
    	A() { cout << "Ctor A" << endl; }
    	~A() { cout << "Dtor A" << endl; }
    };
    class B : virtual public A {
    public:
    	int b;
    	B() { cout << "Ctor B" << endl; }
    	~B() { cout << "Dtor B" << endl; }
    };
    class C : public B {
    public:
    	int c;
    	C() { cout << "Ctor C" << endl; }
    	~C() { cout << "Dtor C" << endl; }
    };
    class D : public B {
    public:
    	int d;
    	D() { cout << "Ctor D" << endl; }
    	~D() { cout << "Dtor D" << endl; }
    };
    class E : public C, public D {
    public:
    	int e;
    	E() { cout << "Ctor E" << endl; }
    	~E() { cout << "Dtor E" << endl; }
    };
    
    void main() {
           E a;
    }
    The output is:
    Ctor A
    Ctor B
    Ctor C
    Ctor B
    Ctor D
    Ctor E
    Dtor E
    Dtor D
    Dtor B
    Dtor C
    Dtor B
    Dtor A

    My question is, why the B Constructor isn't constructed again for D class..
    Other words, why the output isn't like this:
    Ctor A
    Ctor B
    Ctor C
    Ctor B
    Ctor D
    Ctor B
    Ctor E
    Dtor E
    Dtor B
    Dtor D
    Dtor B
    Dtor C
    Dtor B
    Dtor A

    Thanks 4your answers (:
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    18
    Rep Power
    0
    I got it..
    Thanks anyway (:

IMN logo majestic logo threadwatch logo seochat tools logo