顺序:
1,调用按照声明顺序的基类构造函数
2,调用内嵌对象构造函数
3,调用自身构造函数
(总是先人后己嘛,先老人(基类)后客人(内嵌对象),自己家小孩儿就不要管大人之间的事儿(不调用派生类构造函数))
这里C继承B和A,内嵌D,E继承C;调用构造函数情况:
测试:
1 #include<iostream> 2 using namespace std ; 3 class D 4 { 5 public: 6 D(){cout<<"initializing D..."<<endl;} 7 ~D(){cout<<"destroying D..."<<endl;} 8 }; 9 class A 10 { 11 public: 12 A(){cout<<"initializing A..."<<endl;} 13 ~A(){cout<<"destroying A..."<<endl;} 14 }; 15 class B 16 { 17 public: 18 B(){cout<<"initializing B..."<<endl;} 19 ~B(){cout<<"destroying B..."<<endl;} 20 }; 21 class C : private B , public A 22 { 23 public: 24 D d; 25 C(){cout<<"initializing C..."<<endl;} 26 ~C(){cout<<"destroying C..."<<endl;} 27 }; 28 class E: public C 29 { 30 public: 31 E(){cout<<"initializing E..."<<endl;} 32 ~E(){cout<<"destroying E..."<<endl;} 33 }; 34 int main() 35 { 36 C c; 37 return 0 ; 38 }