C++编程:静态成员函数,析构函数的调用

下列程序的输出结果是______。
#include<iostream>
using namespace std;
class Test{
public:
Test(){cnt++;}
~Test(){cnt--;}
static int Count(){return cnt;}
private:
static int cnt;
};
int Test::cnt=0;
int main()
{
cout<<Test::Count()<<' ';
Test t1,t2;
Test*pT3=new Test;
Test*pT4=new Test;
cout<<Test::Count()<<' ';
delete pT4;
delete pT3;
cout<<Test::Count()<<endl;
return 0;
}
为什么结果是0 4 2 ? t1,t2不能调用析构函数吗?

开头初始化cnt=0

cout<<Test::Count()<<' ';->输出0
Test t1,t2; -> +2
Test*pT3=new Test;->+1
Test*pT4=new Test;->+1
cout<<Test::Count()<<' ';-》输出4
delete pT4;-》-1
delete pT3; -》-1
cout<<Test::Count()<<endl;-》输出2

输出没有任何问题

t1 t2 在return 之后确实调用了惜购函数,但是你之后也不输出cnt的结果啊
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-09-17
t1,t2还没退出函数,所以还没有释放,也就没有析构了。
相似回答