您的位置:首页 > 编程语言 > C语言/C++

C++构造和析构执行顺序

2018-01-04 09:12 197 查看

结论:

  构造顺序 :基类=》成员数据=>派生类

  析构顺序 :派生类=》成员=》基类

测试代码

class base
{
public:
base(const string& theInfo):
info(theInfo)
{
cout << "base的默认构造函数" << endl;
}
~base()
{
cout << "base 的析构函数" <<endl;
}
private:
string info;
};

class member
{
public:
member()
{
cout << "成员函数的构造" <<endl;
}
~member()
{
cout << "成员的析构函数" <<endl;
}
};

class derived:public base
{
public:
~derived()
{
cout << "derived的构造函数" << endl;
}
derived(const string& theDInfo):
dInfo(theDInfo),
base(theDInfo),
mem()
{
cout << "derived的构造函数" << endl;
}
private:
member mem;
string dInfo;
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: