Counting objects of the same class

S

Stefan Istrate

Hello,

I have a class A and I want to know at every moment what is the number
of objects of type A. I know that a good solution for this is to add a
static variable (member of class A) and increment it in constructor
and decrement it in destructor. But what if I inherit class A in more
classes (B, C, etc)? Every constructor of a child class will call the
constructor of A, so the counting will fail. What solution do you
recommend me?

Thank you,
Stefan Istrate
 
A

AnonMail2005

Hello,

I have a class A and I want to know at every moment what is the number
of objects of type A. I know that a good solution for this is to add a
static variable (member of class A) and increment it in constructor
and decrement it in destructor. But what if I inherit class A in more
classes (B, C, etc)? Every constructor of a child class will call the
constructor of A, so the counting will fail. What solution do you
recommend me?

Thank you,
Stefan Istrate

You can have more than one constructor (or an argument to a
constructor)
that sets a boolean inside the class telling it whether to count a
particular
instance. Instances of type A can be constructed with the arg set to
true
and other derived classes can set the arg to false.

Of course, you should put the creation of the object behind a function
(i.e. factory) so your client code doesn't have to know about this.

HTH
 
M

Markus Moll

Hi

You can have more than one constructor (or an argument to a
constructor)
that sets a boolean inside the class telling it whether to count a
particular
instance. Instances of type A can be constructed with the arg set to
true
and other derived classes can set the arg to false.

Of course, you should put the creation of the object behind a function
(i.e. factory) so your client code doesn't have to know about this.

virtual base classes can also help:

struct A_counter
{
static int count;
bool count_me;
A_counter(bool count_me = false)
: count_me(count_me)
{
if(count_me) ++count;
}

~A_counter()
{
if(count_me) --count;
}
};

struct A : protected virtual A_counter
{
A() : A_counter(true) {}
};

struct D : A
{
};

Markus
 
M

Markus Moll

Hi
Others have suggested two constructors, one for children and one for
everybody else, this assumes that children are going to use the correct
constructor though and requires that you go through all existing
children and change them.

Not necessarily, as you can rely on default constructors and access
specifiers to make sure that A is the only class that could possibly call
the constructor that invokes counting, while all other classes would do the
right thing without any modification.
In the future, I recommend you make all base classes abstract. I.E., the
only type A objects that should be getting created are those from child
classes of A.

While it's true that you often want interfaces, I cannot see any
justification for that generalization.

Markus
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top