构造函数与析构函数何时执行
2010-05-15 19:27
197 查看
测试代码:
#include<iostream>
using namespace std;
class myclass{
int num;
public:
myclass(int i){num=i;cout<<"initial "<<num<<endl;}
~myclass(){cout<<"del "<<num<<endl;}
};
myclass a(1);
int main()
{
cout<<"This will not first printed/n";
{
myclass b(2);
}
cout<<"This will not the last printed/n";
return 0;
}
测试结果:
initial 1
This will not first printed
initial 2
del 2
This will not the last printed
Press any key to continue
可见,全局类的构造在main之前,全局类的析构函数理论上在main之后,但是测试结果没有显示……具体原因不甚了解。
#include<iostream>
using namespace std;
class myclass{
int num;
public:
myclass(int i){num=i;cout<<"initial "<<num<<endl;}
~myclass(){cout<<"del "<<num<<endl;}
};
myclass a(1);
int main()
{
cout<<"This will not first printed/n";
{
myclass b(2);
}
cout<<"This will not the last printed/n";
return 0;
}
测试结果:
initial 1
This will not first printed
initial 2
del 2
This will not the last printed
Press any key to continue
可见,全局类的构造在main之前,全局类的析构函数理论上在main之后,但是测试结果没有显示……具体原因不甚了解。
相关文章推荐
- C/C++——构造函数、复制构造函数和析构函数的执行时刻
- C++在单继承、多继承、虚继承时,构造函数、复制构造函数、赋值操作符、析构函数的执行顺序和执行内容 【转】 参考度4.6星
- 关于基类和派生类的构造函数和析构函数的执行顺序问题
- 何时调用构造函数,复制构造函数,析构函数
- 派生类的构造函数赋值和析构函数执行顺序
- C++在单继承、多继承、虚继承时,构造函数、复制构造函数、赋值操作符、析构函数的执行顺序和执行内容
- 类继承中构造函数、析构函数的执行顺序
- C++在单继承、多继承、虚继承时,构造函数、复制构造函数、赋值操作符、析构函数的执行顺序和执行内容
- 临时对象构造函数和析构函数的执行时机
- C++类对象在各种内存区的构造函数析构函数执行顺序
- php中构造函数和析构函数执行过程
- 构造函数与析构函数执行顺序及其与虚函数关系
- C++构造函数的执行顺序及析构函数的执行顺序-多种情况:普通继承、虚拟继承、类构造函数调用其它类的构造函数
- 何时执行构造函数和析造函数
- 多重继承-构造函数与析构函数执行顺序
- C++在单继承、多继承、虚继承时,构造函数、复制构造函数、赋值操作符、析构函数的执行顺序和执行内容
- 4.5 派生类构造函数和析构函数的执行顺序举例
- 例4.8 含有对象成员的派生类构造函数和析构函数的执行顺序
- 派生类构造函数和析构函数的执行顺序
- C++在单继承、多继承、虚继承时,构造函数、复制构造函数、赋值操作符、析构函数的执行顺序和执行内容