如何计算类的实例个数

来源:互联网 发布:mac照片不显示导入 编辑:程序博客网 时间:2024/06/05 22:35

在类中用一个静态成员变量,初始化值为0。构造函数中使其值加1,析构函数中使其值减1。这里不考虑构造函数和析构函数中抛出异常的情况。

注:不应该在析构函数中抛出异常。

 

How to count the number of instance of a class

The class consists of a static value which is equal to zero in the beginning. This value will be increased by one in the constructor. In C++ there is destructor as well. When it is called this value should be decreased by 1.