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

时间:2009-12-18 06:08:31

标签: c++

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

用一个简单的例子解释

6 个答案:

答案 0 :(得分:29)

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

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

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

例如,请参阅使用Curiously recurring template pattern的对象计数器:

template <typename T>
struct counter
{
    counter()
    {
        objects_created++;
        objects_alive++;
    }

    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>
{
    // ...
};

完整性用法:

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>::object_created
         << " Y:" << counter<Y>::object_created
         << endl;

    cout << "alive: "
         << " X:" << counter<X>::object_alive
         << " Y:" << counter<Y>::object_alive
         << endl;
}

输出:

created:  X:5 Y:3
alive:  X:1 Y:1

答案 1 :(得分: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;
}

此技术称为CRTP

答案 2 :(得分:4)

对象的数量是多少?如果要计算特定类的对象数,可以使用静态计数器。像下面的东西。在破坏时增加创造和减少的计数器..

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

int A :: counter = 0;

答案 3 :(得分:3)

您必须重载new和delete运算符 计算内存分配。

void * operator new (size_t size)
{
    void * p = malloc (size);
    num_allocations++;
    return p;
}

void operator delete (void * p)
{
    num_deletions++;
    free (p);
}

答案 4 :(得分:0)

你可以在你的类的public:中创建一个计数器变量(假设你在这里谈论从你创建的类中计算对象)

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;

取自ddj.com

答案 5 :(得分:0)

更新代码而不是“ stefanB”解决方案,某些变量处于受保护的访问模式,将无法访问。

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;
        }