您的位置:首页 > 编程语言 > C语言/C++

C++ 统计对象个数

2015-12-29 21:58 477 查看
<span style="font-size:18px;">#include "stdafx.h"
#include<iostream>
using namespace std;

class A
{
public:
A(){++count;};
A(const A& rhs){++count;};

~ A(){--count;};
static int GetCount();
private:
static int count;

};
int A::count =0;

int A::GetCount()
{
return count;
}

int _tmain(int argc, _TCHAR* argv[])
{

A a;
cout<<A::GetCount () <<endl;
A d(a);
cout<<A::GetCount () <<endl;
A c=a;
cout<<A::GetCount () <<endl;
A *b=new A;
cout<<A::GetCount () <<endl;
delete b;
cout<<A::GetCount () <<endl;
return 0;
}</span>


若不添加 copy 构造函数 A(const A& rhs){++count;}; 则对象d、对象c统计不出来
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: