构造函数和析构函数调用顺序强化训练

来源:互联网 发布:js new是什么意思 编辑:程序博客网 时间:2024/04/29 15:45
#include <iostream>using namespace std;class ABCD{public:ABCD(int a, int b,int c){this->a = a;this->b = b;this->c = c;printf("ABCD() construct,a:%d,b:%d,c:%d\n",this->a,this->b,this->c);}~ABCD(){printf("~ABCD() construct,a:%d,b:%d,c:%d\n",this->a,this->b,this->c);}int getA(){return this->a;}protected:private:int a;int b;int c;};class E{public:E():abcd1(1,2,3),abcd2(4,5,6),m(100){cout<<"E() construct"<<endl;}~E(){cout<<"~E() construct"<<endl;}E(const E &obj):abcd1(7,8,9),abcd2(10,11,12),m(100){cout<<"copy construct"<<endl;}protected://private:public:ABCD abcd1;ABCD abcd2;const int m ;};int doThing(E my){printf("doThing()my.abcd1. a:%d\n",my.abcd1.getA());return 0;}int run2(){E mye;doThing(mye);//实参初始化形参 调用拷贝构造函数return 0;}int run3(){printf("run3 strat....\n");//ABCD(400,500,600);//临时对象的生存期 只存在这一行ABCD abcd = ABCD(400,500,600);//对象abcd的生存期在run3函数体内printf("run3 end\n");return 0;}int main(){run2();//run3();system("pause");return 0;}

0 0