我们应该要知道,计数的变量,不能包含在每个对象中,应该是所有对象共享。
如下:只是计数了当前对象的内容,所以不能采用
代码1(定义了普通类型的变量)
class Test
{
public:
Test() :t_count(0)
{
t_count++;
}
Test(Test& a)
{
t_count++;
}
~Test()
{
t_count--;
}
private:
int _a;
int t_count;
};
int main()
{
Test t1, t2;
return 0;