C ++ multi-inheritance: the sequence in which constructors are called in the diamond inheritance
In C ++, multiple inheritance inevitably leads to diamond inheritance, that is, the two base classes of the inheritance class are the same base class at the same time. When an object is created, in what order do they call Constructors. If no virtual inheritance is performed: class Base {public: Base () {cout <"Base default constructor call" <endl;} Base (int I) {cout <"Base parameter constructor call" <endl; cout <I <endl;} virtual ~ Base () {}}; class Base1: public Base {public: Base1 (int I, int j = 0): Base (j) {cout <"Base1 parameter constructor call" <endl; cout <I <endl;} virtual ~ Base1 () {}}; class Base2: public Base {public: Base2 (int I): Base (I) {cout <"Base2 parameter constructor call" <endl; cout <I <endl;} virtual ~ Base2 () {}}; class Drived: public Base1, public Base2 {public: Drived (int a, int B, int c, int d): Base1 (), base2 (B) {} virtual ~ Drived () {}}; virtual inheritance (in virtual inheritance, the call to the Base constructor in Base1 Base2 does not work, and the call to the Base constructor is directly undertaken by the derived class, if Drived is not explicitly specified, the default non-parameter constructor is called.): <br> class Base {public: Base () {cout <"Base default constructor call" <endl;} Base (int I) {cout <"Base parameter constructor call" <endl; cout <I <endl;} virtual ~ Base () {}}; class Base1: virtual public Base {public: Base1 (int I, int j = 0): Base (j) {cout <"Base1 parameter constructor call" <endl; cout <I <endl;} virtual ~ Base1 () {}}; class Base2: virtual public Base {public: Base2 (int I): Base (I) {cout <"Base2 parameter constructor call" <endl; cout <I <endl;} virtual ~ Base2 () {}}; class Drived: public Base1, public Base2 {public: Drived (int a, int B, int c, int d): Base1 (), base2 (B) {} virtual ~ Drived () {}}; if the Drived class has a member function of the Base1 Base2 type: class Drived: public Base1, public Base2 {public: Base1 mem1; Base2 mem2; Drived (int, int B, int c, int d): Base1 (a), Base2 (B), mem1 (c), mem2 (d) {} virtual ~ Drived () {}}; if you specify the Base constructor In the Derived class: class Drived: public Base1, public Base2 {public: Base1 mem1; Base2 mem2; Drived (int, int B, int c, int d): Base1 (a), Base2 (B), Base (a), mem1 (c), mem2 (d) {} virtual ~ Drived (){}};