C++ 统计一个类对象的个数

来源:互联网 发布:p身份证的软件 编辑:程序博客网 时间:2024/05/29 13:33
#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 main(){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;}


若不添加  copy 构造函数   A(const A& rhs){++count;}; 则对象d、对象c统计不出来

0 0
原创粉丝点击