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

前端 未结 6 1453
一个人的身影
一个人的身影 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 T>
    class Counter
    {
      private:
          static int count;
      public:
        Counter()
        {
           count++;
        }  
        Counter(const Counter &c)
        {
           count++;
        }   
        ~Counter()
        {
           count--;
        }    
        static int GetCount() {
    
             return count;
        }
    }
    
    template<class T> 
    int Counter<T>::count = 0; 
    
    
    
    class MyClass : private Counter<MyClass>
    {
       public:
          using Counter<MyClass>::GetCount;
    }
    

    This technique is called CRTP

    0 讨论(0)
  • 2020-12-13 21:54

    update on code as opposed to "stefanB" solution, some variables are in protected access mode and will not be accessible.

    template <typename T>
        struct counter
        {
            counter()
            {
                std::cout << "object created"<< endl;
                this->objects_created++;
                this->objects_alive++;
            }
    
            counter(const counter& test)
            {
                std::cout << "object created:" << test << endl;
                 this->objects_created++;
                 this->objects_alive++;
            }
    
            static int getObjectsAlive(){
                return objects_alive;
            }
    
            static int getObjectsCreated(){
                return objects_created;
            }
    
    
        protected:
            virtual ~counter()
            {
                std::cout << "object destroyed"<< endl;
                --objects_alive;
            }
            static int objects_created;
            static int objects_alive;
        };
        template <typename T> int counter<T>::objects_created( 0 );
        template <typename T> int counter<T>::objects_alive( 0 );
    
        class X : counter<X>
        {
            // ...
        };
    
        class Y : counter<Y>
        {
            // ...
        };
        
        Usage for completeness:
        
            int main()
            {
                X x1;
            
                {
                    X x2;
                    X x3;
                    X x4;
                    X x5;
                    Y y1;
                    Y y2;
                }   // objects gone
            
                Y y3;
             
        cout << "created: "
             << " X:" << counter<X>::getObjectsCreated()
             << " Y:" << counter<Y>::getObjectsCreated()  //well done
             << endl;
    
        cout << "alive: "
             << " X:" << counter<X>::getObjectsAlive()
             << " Y:" << counter<Y>::getObjectsAlive()
             << endl;
            }
    
    0 讨论(0)
  • 2020-12-13 21:55

    You could create a counter variable into the public: of your class (assuming here you're talking about counting objects from a class you created)

    class Widget {
    public:
        Widget() { ++count; }
        Widget(const Widget&) { ++count; }
        ~Widget() { --count; }
    
        static size_t howMany()
        { return count; }
    
    private:
        static size_t count;
    };
    // obligatory definition of count. This
    // goes in an implementation file
    size_t Widget::count = 0;
    

    Taken from ddj.com

    0 讨论(0)
  • Create template class with a static counter.

    Each object in your application would then extend this template class.

    When constructor is called increment static count (static variable is per class - shared by all objects of that class).

    For example see Object Counter using Curiously recurring template pattern:

     template <typename T>
        struct counter
        {
            counter()
            {
                objects_created++;
                objects_alive++;
            }
    
            counter(const counter&)
            {
                 objects_created++;
                 objects_alive++;
            }   
    
        protected:
            virtual ~counter()
            {
                --objects_alive;
            }
            static int objects_created;
            static int objects_alive;
        };
        template <typename T> int counter<T>::objects_created( 0 );
        template <typename T> int counter<T>::objects_alive( 0 );
    
        class X : counter<X>
        {
            // ...
        };
    
        class Y : counter<Y>
        {
            // ...
        };
    
    Usage for completeness:
    
        int main()
        {
            X x1;
    
            {
                X x2;
                X x3;
                X x4;
                X x5;
                Y y1;
                Y y2;
            }   // objects gone
    
            Y y3;
    
            cout << "created: "
                 << " X:" << counter<X>::objects_created
                 << " Y:" << counter<Y>::objects_created  //well done
                 << endl;
    
            cout << "alive: "
                 << " X:" << counter<X>::objects_alive
                 << " Y:" << counter<Y>::objects_alive
                 << endl;
        }
    

    Output:

    created:  X:5 Y:3
    alive:  X:1 Y:1
    
    0 讨论(0)
  • 2020-12-13 22:12

    You have to overload the new and delete operators to count memory allocations.

    void * operator new (size_t size)
    {
        void * p = malloc (size);
        num_allocations++;
        return p;
    }
    
    void operator delete (void * p)
    {
        num_deletions++;
        free (p);
    }
    
    0 讨论(0)
  • 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;
    
    0 讨论(0)
提交回复
热议问题