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

前端 未结 6 1455
一个人的身影
一个人的身影 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 21:52

    template 
    class Counter
    {
      private:
          static int count;
      public:
        Counter()
        {
           count++;
        }  
        Counter(const Counter &c)
        {
           count++;
        }   
        ~Counter()
        {
           count--;
        }    
        static int GetCount() {
    
             return count;
        }
    }
    
    template 
    int Counter::count = 0; 
    
    
    
    class MyClass : private Counter
    {
       public:
          using Counter::GetCount;
    }
    

    This technique is called CRTP

提交回复
热议问题