标签:alt clu on() 图片 space size 去掉 成员 12px
//Test1.h #include<iostream> using namespace std; class Base1 { private: int a1; public: Base1()//(int _a):a1(_a) { cout<<"It‘s base1 built. "<<endl; } ~Base1(){cout<<"Base1 was free. "<<endl;} }; class Base2 { private: int a2; public: Base2()//(int _a):a2(_a) { cout<<"It‘s base2 built. "<<endl; } ~Base2(){cout<<"Base2 was free. "<<endl;} }; class Base3 { private: int a3; public: Base3()//(int _a):a3(_a) { cout<<"It‘s base3 built. "<<endl; } ~Base3(){cout<<"Base3 was free. "<<endl;} }; class Son : public Base2,public Base1,public Base3//1.调用基类的构造函数,按他们在派生类定义的先后顺序,顺序调用。 { private://2.调用成员对象的构造函数,按他们在类定义中声明的先后顺序,顺序调用 Base1 a; Base3 b; Base2 c; public: Son()//:Base1(_a),Base2(_a),Base3(_a),a(_a),b(_a),c(_a) { cout<<"It‘s son built. "<<endl;//3.派生类的构造函数体中的操作 } ~Son(){cout<<"Son was free. "<<endl;} };
将父类和派生类构造函数后的“//”去掉便是
在派生类构造函数中,只要基类不是使用缺省构造函数,都要显式给出基类名和参数表
所描述的意思。
//Test.cpp
#include"Test1.h" void main() { Son son;//son(10) }
由Son类可以看出构造函数的顺序应该为2,1,3,1,3,2,son
运行结果
析构函数和构造函数顺序相反。
标签:alt clu on() 图片 space size 去掉 成员 12px
原文地址:https://www.cnblogs.com/area-h-p/p/10349091.html