标签:部件 struct vat return stream name top space 析构函数
文章来源:https://bbs.csdn.net/topics/390253882
“构造函数调用顺序:先调用内嵌对象的构造函数(按内嵌时的声明顺序,先声明者先构造)。然后调用本类的构造函数。(析构函数的调用顺序相反)”
#include<iostream> using namespace std; class Part //部件类 { public: Part() {cout<<"Part default construction called"<<endl;} Part(int i) {cout<<"Part: "<<i<<" construction called"<<endl;} ~Part() {cout<<"Part deconstruction called"<<endl;} private: int val; }; class Whole { public: Whole(); Whole(int i,int j,int k); ~Whole(); private: //这三个变量的声明顺序改变,打印输出的结果仍然一样 Part one; Part two; int date; }; Whole::Whole() { date=0;} Whole::Whole(int i,int j,int k) :one(j),date(k),two(i) //这里的初始化顺序按照one(2),date(3),two(1)进行,按照i,j,k的顺序来的. :/*one(j),*/date(k), two(i) //默认构造one { cout<<"Whole‘s construction called!"<<endl; } Whole::~Whole() { cout<<"Whole deconstruction called!"<<endl; } int main(void) { Whole w(1, 2, 3); return 1; }
输出结果:
Part: 2 construction called
Part: 1 construction called
Whole‘s construction called!
Whole deconstruction called!
Part deconstruction called
Part deconstruction called
“构造函数调用顺序:先调用内嵌对象的构造函数(按内嵌时的声明顺序,先声明者先构造,比如上面的按照i,j,k)。然后调用本类的构造函数。(析构函数的调用顺序相反)”
标签:部件 struct vat return stream name top space 析构函数
原文地址:https://www.cnblogs.com/Stephen-Qin/p/11512427.html