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 5 1932
On Jul 17, 10:48*am, Stefan Istrate <stefan.istr...@gmail.comwrote:
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
Hi An**********@gmail.com wrote:
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
Stefan Istrate <st************@gmail.comwrote:
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.
Why do you consider that a failure, objects of child classes are also
type A objects...
What solution do you recommend me?
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.
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.
Hi
Daniel T. wrote:
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
Markus Moll <ma*********@esat.kuleuven.ac.bewrote:
Daniel T. wrote:
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.
If you have a class "A" and you make a sub-class of it "B" you also
(whether you want to or not) implicitly make another "sub-class" (all
A's that are not also B's.) In other words, the generalization
implicitly exists, whether you can see the justification for it or not.
This very problem is a perfect example, the OP wants to count all A's
that are not also B's (or C's or whatever.) If he had a class to
explicitly represent this concept, the solution would have been so
straight forward, he probably wouldn't have had to ask how. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
by: stephan beal |
last post by:
Good morning, C++ers,
A couple days ago i came across a useful trick for counting instances of a
given class, and i thought i'd pass it on:
Conventionally, as described in several books, to do...
|
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...
|
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...
|
by: aaronfude |
last post by:
Hi,
Please consider the following class (it's not really my class, but it's
a good example for my question):
class Vector {
int myN;
double *myX;
Vector(int n) : myN(n), myX(new double) { }...
|
by: Ole Nielsby |
last post by:
I need to implement reference counting in a class hierarch,
in a thread safe manner. (The classes are umanaged but I
might want to compile them with the /clr option.)
Some of the objects - atoms...
|
by: oec.deepak |
last post by:
Hi
Cn any one telll me what is Reference counting in C++.
|
by: colin |
last post by:
Hi,
I need to cache various resource objects such as images for example,
wich might be referenced from multiple objects
(wich could be considered documents for ease of discusion)
and the idea is...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |