how to count the number of objects created in c++
pls explain with a simple example
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