#include<iostream>
using namespace std;
class A{
public:
static int cnt;
A()
{
++cnt;
cout<<"constructor:"<<cnt<<endl;
}
~A()
{
--cnt;
cout<<"destructor:"<<cnt<<endl;
}
};
int A::cnt = 0;
A f(A x){
return x;
}
int main(){
A a0;
A a1 = f(a0);
return 0;
}
该程序将输出:
构造函数:1 析构函数:0 析构函数:-1 析构函数:-2
构造函数和析构函数不是成对出现的吗?