The general sequence of constructing a class object is:
(1): if there is a direct or indirect virtual base class, execute the constructor of the virtual base class first.
(2): If it is a base class, they execute their constructor in the order they appear in the inherited declaration list, but during the constructor, the constructor of the virtual base class is no longer executed.
[Csharp]
# Include <iostream>
Using namespace std;
Class base0 {
Public: www.2cto.com
Base0 (int var): var0 (var) {cout <"ggggg" <endl ;};
Int var0;
Void fun0 () {cout <"memory of base0" <endl ;}
~ Base0 () {cout <"gggg" <endl ;}
};
Class base1: virtual public base0 {
Public:
Base1 (int var): base0 (var) {cout <"hhhhh" <endl ;}
Int var1;
~ Base1 () {cout <"HHHH" <endl ;}
};
Class base2: virtual public base0 {
Public:
Base2 (int var): base0 (var) {cout <"tttttt" <endl ;}
Int var2;
};
Class mm: public base1, public base2 {
Public:
Mm (int var): base1 (var), base0 (var), base2 (var) {cout <"ppppp" <endl ;}
Int var;
Void fun () {cout <"member of mm" <endl ;}
~ Mm () {cout <"ppppp" <endl ;}
};
Int main (){
Mm a (1 );
A. var = 2;
A. fun ();
Return 0;
}