如何计算在c ++中创建的对象数量

Aru*_*lam 13 c++

如何计算在c ++中创建的对象数量

请用一个简单的例子来解释

ste*_*anB 30

使用静态计数器创建模板类.

然后,应用程序中的每个对象都将扩展此模板类.

当构造函数被称为增量静态计数时(静态变量是每个类 - 由该类的所有对象共享).

例如,使用奇怪的重复模板模式查看对象计数器:

 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;
    }
Run Code Online (Sandbox Code Playgroud)

用法完整性:

created:  X:5 Y:3
alive:  X:1 Y:1
Run Code Online (Sandbox Code Playgroud)

输出:

 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;
    }
Run Code Online (Sandbox Code Playgroud)

  • 这是怎么回事.X x2; X x3 = x2; (2认同)

Ash*_*ish 12

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;
}
Run Code Online (Sandbox Code Playgroud)

这种技术称为CRTP

  • @stefanB,这是正确的方法.你需要在Counter中有复制构造函数. (3认同)

bdh*_*har 4

物体的数量有什么用?如果要统计特定类的对象数量,可以使用静态计数器。像下面这样的东西..创建时递增计数器,销毁时递减计数器..

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

int A :: counter = 0;
Run Code Online (Sandbox Code Playgroud)