473,405 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Counting Objects - Best Method

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
Jul 22 '05 #1
6 5187
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.
"Anon Email" <an********@fastmail.fm> wrote in message
news:83*************************@posting.google.co m...
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

Jul 22 '05 #2

"Anon Email" <an********@fastmail.fm> wrote in message
news:83*************************@posting.google.co m...
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.
Jul 22 '05 #3
On 21 Dec 2003 00:33:43 -0800, an********@fastmail.fm (Anon Email)
wrote:
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
Jul 22 '05 #4
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
Jul 22 '05 #5

"Anon Email" <an********@fastmail.fm> wrote in message
news:83*************************@posting.google.co m...
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.

Jul 22 '05 #6
Thanks guys,

Deets
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Elbert Lev | last post by:
Please correct me if I'm wrong. Python (as I understand) uses reference counting to determine when to delete the object. As soon as the object goes out of the scope it is deleted. Python does...
1
by: ash | last post by:
hi does anyone has any experience with flyweight pattern with refernce counting i want to share objects between multiple clients and want to delete the object from shared pool when the last...
14
by: Mike N. | last post by:
Hello: I have a form that contains a multiple-select field that has 12 options in it. I would like the user to be able to select UP TO FOUR of those options. If they select more than four, I...
7
by: Bambero | last post by:
Hello all Problem like in subject. There is no problem when I want to count days between two dates. Problem is when I want to count years becouse of leap years. For ex. between 2002-11-19...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
7
by: sathyashrayan | last post by:
Group, Following function will check weather a bit is set in the given variouble x. int bit_count(long x) { int n = 0; /* ** The loop will execute once for each bit of x set,
10
by: cj | last post by:
I'm writing a TCP/IP server app that will have many simultaneous connections. The main thread listens for new connections and starts a thread to handle each requested connection. These are short...
1
by: oec.deepak | last post by:
Hi Cn any one telll me what is Reference counting in C++.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.