#include <iostream>
#include <cstdio>
using namespace std;
class T{
public:
T()
{
cout << "constuct" << endl;
}
~T()
{
cout << "destroy" << endl;
}
};
T fun()
{
static T t;
return t;
}
int main()
{
T t = fun();
int mark;
cin >> mark;
cout << mark << endl;
return 0;
}
结果是(不用因只有一个construct,有两个destroy感到奇怪,因为T t = fun()调用了默认复制构造函数):
constuct
1
1
destroy
destroy
而不是:
constuct
destroy
1
1
destroy