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统计不出来
相关文章推荐
- C++整行读取.txt文件
- 实战c++中的智能指针unique_ptr系列--通过unique_ptr对shared_ptr进行初始化
- 实战c++中的智能指针unique_ptr系列--通过unique_ptr对shared_ptr进行初始化
- [code segments] OpenCV3.0 SVM with C++ interface
- [code segments] OpenCV3.0 SVM with C++ interface
- Sicily 1134. 积木分发
- 【LVL1_6_c】【思考】X86 c语言结构体数据对齐的规则
- C语言结构体内存空间分配
- C++新增数据类型long long ,unsigned long long
- C++命名空间
- DevC++ 开debug
- 考考你的C语言
- c语言之遗漏---标准C的标记化结构初始化语法
- 《C++Primer 5e》学习笔记(4):语句
- 深入理解C++中函数参数——传值与传址详解
- 【笨木头C++】基与基础10:又是函数之引用
- C++ Queues(队列)、Priority Queues(优先队列)
- C语言 字符串操作函数
- 我推荐的一些C\C++书籍
- Something about C