Counting Objects - Best Method



 DEVELOP > c-Plus-Plus > Counting Objects - Best Method

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Anon Email"
Date: 21 Dec 2003 02:33:43 AM
Object: 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
.

User: "Sumit Rajan"

Title: Re: Counting Objects - Best Method 21 Dec 2003 03:27:41 AM
"Anon Email" <anonemail1@fastmail.fm> wrote in message
news:83b3ca3.0312210033.61a0d135@posting.google.com...

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.
.
User: "Anon Email"

Title: Re: Counting Objects - Best Method 21 Dec 2003 07:59:31 PM
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
.
User: "Sumit Rajan"

Title: Re: Counting Objects - Best Method 23 Dec 2003 12:31:18 AM
"Anon Email" <anonemail1@fastmail.fm> wrote in message
news:83b3ca3.0312211759.7ae37649@posting.google.com...

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.
.



User: "Moonlit"

Title: Re: Counting Objects - Best Method 21 Dec 2003 03:14:44 AM
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" <anonemail1@fastmail.fm> wrote in message
news:83b3ca3.0312210033.61a0d135@posting.google.com...

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

.

User: "tom_usenet"

Title: Re: Counting Objects - Best Method 21 Dec 2003 04:27:26 PM
On 21 Dec 2003 00:33:43 -0800,
(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
.
User: "Anon Email"

Title: Re: Counting Objects - Best Method 23 Dec 2003 07:59:11 PM
Thanks guys,
Deets
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER