how to count the number of objects created in c++

前端 未结 6 1461
一个人的身影
一个人的身影 2020-12-13 21:14

how to count the number of objects created in c++

pls explain with a simple example

6条回答
  •  感动是毒
    2020-12-13 22:17

    Number of objects for what? If you want to count the number of objects of a specific class, you can use a static counter. Something like below.. Increment counter on creation and decrement while destruction..

    class A
    {
      public:
        static int counter;
        A()
        {
          counter ++;
        }
        virtual ~A()
        {
          counter --;
        }
    };
    
    int A :: counter = 0;
    

提交回复
热议问题