Counting Objects - Best Method

A

Anon Email

Hi everyone,

I've written some code that contains a class, and I want to track the
number of instances of this class that have been created, eg.: "This
is number xx object created." What is the most commonly accepted
method to do this? By defining a separate "counter" class? Do I need
to use a reference/ references?

Cheers,

Deets
 
M

Moonlit

Hi,

Create a static variable (or class instance) Then in the contstructor
increment the counter and in the destructor decrement.

If you want to make it thread safe make sure to protect the counter with a
mutex.

Regards, Ron AF Greve.
 
S

Sumit Rajan

Anon Email said:
Hi everyone,

I've written some code that contains a class, and I want to track the
number of instances of this class that have been created, eg.: "This
is number xx object created." What is the most commonly accepted
method to do this? By defining a separate "counter" class? Do I need
to use a reference/ references?

You may find the following helpful:

#include <iostream>

struct Test{
static int tc; //Total number of instances
int i_number;
Test() {i_number=++tc;}

};

int Test::tc = 0;

int main()
{

Test a,b,c,d;
std::cout << "Total number of instances: " << a.tc << '\n';
std::cout << "\'c\' is instance number: " << c.i_number << '\n';
}

Regards,
Sumit.
 
T

tom_usenet

Hi everyone,

I've written some code that contains a class, and I want to track the
number of instances of this class that have been created, eg.: "This
is number xx object created." What is the most commonly accepted
method to do this? By defining a separate "counter" class? Do I need
to use a reference/ references?

template <class T>
class InstanceCounter
{
static int s_instances;
public:
InstanceCounter()
{
++s_instances;
}

~InstanceCounter()
{
--s_instances;
}

InstanceCounter(InstanceCounter const&)
{
++s_instances;
}

static int getCount()
{
return s_instances;
}
};

template <class T>
int InstanceCounter<T>::s_instances = 0;

You could make it threadsafe by using appropriate atomic operations
rather than ++/--. To use it:

class myclass: private InstanceCounter<myclass>
{
};

and check the count with InstanceCounter<myclass>::getCount().

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
A

Anon Email

Thanks heaps, guys. People are always so helpful in this forum.

To Ron:
Create a static variable (or class instance)

Do you mean either:

1) create the counter as a static variable OR
2) create it as a separate class?
Then in the constructor increment the counter and in the destructor
decrement.

This would only be good for tracking the number of objects in
existence, wouldn't it? I want to keep track of the total number of
objects created, regardless of whether or not they've been destroyed.

-----------------

To Sumit:

Thanks for that code. Now I understand static variables. A question:
is this good code? I know it works, but is it written according to
best practice?

Another question: In your call to the static variable you specify an
object - i.e. "a.tc". Is there a way to do this without calling a
specific object?

Cheers,

Deets
 
S

Sumit Rajan

Anon Email said:
To Sumit:

Thanks for that code. Now I understand static variables. A question:
is this good code? I know it works, but is it written according to
best practice?


I 'm not really sure whether it is good code. I was hoping that someone
would comment on it. Wouldn't it be better if we used a class instead of a
struct?
Something like:

#include <iostream>

class Test{
static int tc; //Total number of instances
int i_number;
public:
Test() {i_number=++tc;}
int get_instance_number() const;
static int get_total_instances();
};

int Test::tc = 0;

int Test::get_total_instances()
{
return tc;
}

int Test::get_instance_number() const
{
return i_number;
}

int main()
{

Test a,b,c,d;
std::cout << "Total number of instances: " <<
Test::get_total_instances() << '\n';
std::cout << "\'c\' is instance number: " << c.get_instance_number() <<
'\n';
}

Another question: In your call to the static variable you specify an
object - i.e. "a.tc". Is there a way to do this without calling a
specific object?

You're right: using a.tc in the previous post was a bad idea.
You could use Test::tc there. In the above example, you could use
Test::get_total_instances().

Regards,
Sumit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top