I have a question concerning an article by Scott Meyers, C/C++ Users
Journal, April 1998, at http://www.cuj.com/documents/s=8066/cuj9804meyers/
He explains how to count objects of a given type. Inheritance is preferred
over containment, but then the "old virtual destructor spiel" comes up. Read
it. Now my silly question is, why is the destructor not declared virtual in
the template?
Here is a quote from the article:
<quote>
We can get the behavior we want by employing one of the best-known but
oddest-named tricks in all of C++: we turn Counter into a template, and each
class using Counter instantiates the template with itself as the template
argument.
Let me say that again. Counter becomes a template:
template<typename T>
class Counter {
public:
Counter() { ++count; }
Counter(const Counter&) { ++count; }
~Counter() { --count; }
static size_t howMany()
{ return count; }
private:
static size_t count;
};
template<typename T>
size_t
Counter<T>::count = 0; // this now can go in header
</quote>
The above becomes:
template<typename T>
class Counter {
public:
Counter() { ++count; }
Counter(const Counter&) { ++count; }
virtual ~Counter() { --count; }
static size_t howMany()
{ return count; }
private:
static size_t count;
};
template<typename T>
size_t
Counter<T>::count = 0; // this now can go in header
-X 6 4498
"Agent Mulder" <mb*******************@home.nl> wrote... I have a question concerning an article by Scott Meyers, C/C++ Users Journal, April 1998, at
http://www.cuj.com/documents/s=8066/cuj9804meyers/
He explains how to count objects of a given type. Inheritance is preferred over containment, but then the "old virtual destructor spiel" comes up.
Read it. Now my silly question is, why is the destructor not declared virtual
in the template?
Because there is no need in it in his example. He never deletes
an object of a derived class through a pointer to the base class.
Victor
Agent Mulder escribió: http://www.cuj.com/documents/s=8066/cuj9804meyers/ He explains how to count objects of a given type. Inheritance is preferred over containment, but then the "old virtual destructor spiel" comes up.Read it. Now my silly question is, why is the destructor not declared virtual in the template?
Because he expect that nothing delete derived objects using a pointer to
Counter, and does not want to impose the need of an vtable in all
classes that uses Counter.
Regards.
AM> why is the destructor not declared virtual in the template?
JA> Because he expect that nothing delete derived objects using a pointer
to
JA> Counter, and does not want to impose the need of an vtable in all
JA> classes that uses Counter.
That's correct. He implements it with 0 byte overhead, very neat. But
I want to find out more about the version that does declare a virtual
destructor because:
1. the classes that use the template are base classes themselves and already
contain
virtual functions
2. I am willing to pay a few bytes for this kind of behaviour
3. it looks like this crazy idiom has more to it
I gave the original code the typical "java-shave". Remember the asymmetrical
beard, in vogue not so long ago.
Still I am missing a point, because I expect the constructors
of classes Jazz, Funk and Bach to be invoked:
#include<iostream>
template<typename A>class Counter
{
private:static int count;
public:Counter(){++count;}
public:Counter(const Counter&){++count;}
public:virtual~Counter(){--count;}
public:static int howMany(){return count;}
};
template<typename A>int Counter<A>::count=0;
class Jazz{public:Jazz(){std::cout<<"\tSalt Peanuts!";}};
class Funk{public:Funk(){std::cout<<"\tP-Funk";}};
class Bach{public:Bach(){std::cout<<"\tH-C-A-B";}};
int main(int argc,char**argv)
{
Counter<Jazz>a,b,c,d;
Counter<Funk>e,f,g;
Counter<Bach>h,i,j,k,l,m;
std::cout<<"\nJazz.count = "<<Counter<Jazz>::howMany();
std::cout<<"\nBach.count = "<<Counter<Bach>::howMany();
std::cout<<"\nFunk.count = "<<Counter<Funk>::howMany();
return 0;
}
_____
output
Jazz.count = 4
Bach.count = 6
Funk.count = 3
-X
Agent Mulder wrote: AM> why is the destructor not declared virtual in the template?
JA> Because he expect that nothing delete derived objects using a pointer to JA> Counter, and does not want to impose the need of an vtable in all JA> classes that uses Counter.
That's correct. He implements it with 0 byte overhead, very neat. But I want to find out more about the version that does declare a virtual destructor because:
1. the classes that use the template are base classes themselves and already contain virtual functions 2. I am willing to pay a few bytes for this kind of behaviour 3. it looks like this crazy idiom has more to it
I gave the original code the typical "java-shave". Remember the asymmetrical beard, in vogue not so long ago. Still I am missing a point, because I expect the constructors of classes Jazz, Funk and Bach to be invoked:
Why? An object of any of those classes is never constructed either
explicitly or implicitly. #include<iostream> template<typename A>class Counter { private:static int count; public:Counter(){++count;} public:Counter(const Counter&){++count;} public:virtual~Counter(){--count;} public:static int howMany(){return count;} }; template<typename A>int Counter<A>::count=0;
The Counter template never instantiates or uses an object of the class
it is intended to count.
class Jazz{public:Jazz(){std::cout<<"\tSalt Peanuts!";}}; class Funk{public:Funk(){std::cout<<"\tP-Funk";}}; class Bach{public:Bach(){std::cout<<"\tH-C-A-B";}}; int main(int argc,char**argv) { Counter<Jazz>a,b,c,d; Counter<Funk>e,f,g; Counter<Bach>h,i,j,k,l,m;
Thus explicit instantiation of an object of type Counter<A> does not
imply instantiation of an object of type A (where A = {Jazz, Funk,
Back}). So of course it is possible to count objects that don't exist
but such destructive programming was not considered by the author of the
article to which you refer.
std::cout<<"\nJazz.count = "<<Counter<Jazz>::howMany(); std::cout<<"\nBach.count = "<<Counter<Bach>::howMany(); std::cout<<"\nFunk.count = "<<Counter<Funk>::howMany(); return 0; } _____ output Jazz.count = 4 Bach.count = 6 Funk.count = 3
-X
HTH,
Andrew Heath
AH> such destructive programming was not considered by the author of the
AH> article to which you refer.
Destructive? Can you make some slight modifications so that the
constructors of Bach, Jazz and Funk do get invoked? Is it just
because I gave the original code the typical "java-shave"? Remember
the a-symmetrical beard that was in vogue not so long ago. Fashion
comes and goes.
#include<iostream>
class Bach{public:Bach(){std::cout<<"\tH-C-A-B";}};
class Jazz{public:Jazz(){std::cout<<"\tSalt Peanuts!";}};
class Funk{public:Funk(){std::cout<<"\tP-Funk";}};
template<typename A>class Counter
{
private:static int count;
public:Counter(){++count;}
public:Counter(const Counter&){++count;}
public:virtual~Counter(){--count;}
public:static int howMany(){return count;}
};
template<typename A>int Counter<A>::count=0;
int main(int argc,char**argv)
{
Counter<Jazz>a,b,c,d;
Counter<Funk>e,f,g;
Counter<Bach>h,i,j,k,l,m;
std::cout<<"\nJazz.count = "<<Counter<Jazz>::howMany();
std::cout<<"\nBach.count = "<<Counter<Bach>::howMany();
std::cout<<"\nFunk.count = "<<Counter<Funk>::howMany();
return 0;
}
_____
output
Jazz.count = 4
Bach.count = 6
Funk.count = 3
-X
"Agent Mulder" <mb*******************@home.nl> wrote in message
news:bi**********@news1.tilbu1.nb.home.nl... AH> such destructive programming was not considered by the author of the AH> article to which you refer.
Destructive? Can you make some slight modifications so that the constructors of Bach, Jazz and Funk do get invoked? Is it just because I gave the original code the typical "java-shave"? Remember the a-symmetrical beard that was in vogue not so long ago. Fashion comes and goes.
#include<iostream> class Bach{public:Bach(){std::cout<<"\tH-C-A-B";}}; class Jazz{public:Jazz(){std::cout<<"\tSalt Peanuts!";}}; class Funk{public:Funk(){std::cout<<"\tP-Funk";}}; template<typename A>class Counter { private:static int count; public:Counter(){++count;} public:Counter(const Counter&){++count;} public:virtual~Counter(){--count;} public:static int howMany(){return count;} }; template<typename A>int Counter<A>::count=0; int main(int argc,char**argv) {
/* Counter<Jazz>a,b,c,d; Counter<Funk>e,f,g; Counter<Bach>h,i,j,k,l,m;
*/
Jazz a,b,c,d; // Construct objects of type Jazz. This will invoke the
// constructor of Jazz
Funk e,f,g; // Likewise with Funk here
Bach h,i,j,k,l,m; // and Bach here. std::cout<<"\nJazz.count = "<<Counter<Jazz>::howMany(); std::cout<<"\nBach.count = "<<Counter<Bach>::howMany(); std::cout<<"\nFunk.count = "<<Counter<Funk>::howMany(); return 0; } _____ output Jazz.count = 4 Bach.count = 6 Funk.count = 3
-X
The declaration
Counter<T> a;
invokes the constructor of Counter<T>. Since Counter<T> has no base, no
(non-static) member variables, and its constructor has no local variables,
no other constructor is invoked by this declaration.
/kv 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: 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: Dan |
last post by:
Is this discouraged?:
for line in open(filename):
<do something with line>
That is, should I do this instead?:
fileptr = open(filename)
for line in fileptr:
<do something with line>
|
by: oec.deepak |
last post by:
Hi
Cn any one telll me what is Reference counting in C++.
|
by: mdoxdo |
last post by:
Hi all,
I was wondering if there is a way to get the number of references an
objects have (ie, other objects reference that object)? I know
that .Net GC uses a different mechanism than the...
|
by: Stefan Istrate |
last post by:
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...
|
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: 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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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,...
| |