C++类中的成员可以是另一个类的对象,我们称之为对象成员。两个类对象的执行顺序是什么呢?
程序源码
#include <iostream>
using namespace std;
class HA
{
public:
HA()
{
cout << "HA construct" << endl;
}
~HA()
{
cout << "HA destroy" << endl;
}
};
class HB
{
public:
HB()
{
cout << "HB construct" << endl;
}
~HB()
{
cout << "HB destroy" << endl;
}
//包含HA类,作为其成员变量
private:
HA ha1;
};
int main()
{
HB hb1;
return 0;
}
结果
总结
构造步骤:先有零件,后有整体。析构与构造逻辑相反。