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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1926605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:37:05  来源:igfitidea点击:

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

c++

提问by Arunachalam

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

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

pls explain with a simple example

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

回答by stefanB

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:

例如,请参阅使用Curiously recurring 模板模式的对象计数器:

 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

回答by Ashish

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

这种技术称为CRTP

回答by bdhar

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;

回答by DevDevDev

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

您必须重载 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);
}

回答by Alex Marcotte

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)

您可以在 public: of your class 中创建一个 counter 变量(假设您在这里谈论的是从您创建的类中计算对象)

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

摘自ddj.com