/*******************************************************
*author:彭晓林
*copyright: 版权所有,翻版不究
*function: 类参数构造顺序,析构顺序参数测试
******************************************************/
#include <iostream>
#include <string>
using namespace std;
class DEMO
{
public:
DEMO (int a){cout<<"调用构造函数:"<<a<<endl; x = a;}
~DEMO(){cout<<"调用析构函数"<<x<<endl;}
private:
int x;
};
class SUM
{
public:
SUM(int a, int b):y(a),x(b)
{
cout<<"SUM constractor"<<endl;
}
void print();
private:
DEMO x;
DEMO y;
};
int main()
{
SUM *p = new SUM(1,2);
delete p;
while(1);
}s
本文通过一个具体的C++示例程序,展示了类构造函数与析构函数的调用顺序,特别是当这些函数应用于带有成员对象的类时的顺序。通过对SUM类的构造过程进行跟踪,可以观察到构造函数与析构函数的执行流程。

被折叠的 条评论
为什么被折叠?



