1 /*曾经有段时间一直被构造函数中的虚函数所困扰,现在通过自己重新学习了一遍,标注一下容易忘记的知识*/ 2 #include<iostream> 3 using namespace std; 4 class Base0 5 { 6 public: 7 Base0(int var):var0(var) 8 { 9 cout<<"Construct base0"<<endl; 10 } ; 11 int var0; 12 void fun() 13 { 14 cout<<"Menber of Base0"<<endl; 15 } 16 17 }; 18 class Base1 :virtual public Base0 19 { 20 public: 21 Base1(int var) :Base0(var) 22 { 23 cout<<"Construct base1"<<endl; 24 } 25 int var1 ; 26 }; 27 class Base2 : virtual public Base0 28 { 29 public : 30 Base2(int var): Base0(var) 31 { 32 cout<<"Construct base2"<<endl; 33 } 34 int var; 35 }; 36 class Derived : public Base2,public Base1 //构造函数的执行顺序与这儿有关 37 { 38 public : 39 Derived(int var) : Base2(var),Base1(var),Base0(var) 40 { 41 cout<<"Construct Derived"<<endl; 42 } 43 int var; 44 void fun() 45 { 46 cout<<"Member of Derived"<<endl; 47 } 48 }; 49 int main() 50 { 51 Derived d(1); 52 d.var=2; 53 d.fun(); 54 return 0; 55 }