473,396 Members | 1,833 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,396 software developers and data experts.

counting objects

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
Jul 19 '05 #1
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
Jul 19 '05 #2
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.
Jul 19 '05 #3
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
Jul 19 '05 #4
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

Jul 19 '05 #5
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


Jul 19 '05 #6
"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
Jul 19 '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...
1
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...
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...
3
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...
14
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>
1
by: oec.deepak | last post by:
Hi Cn any one telll me what is Reference counting in C++.
8
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...
5
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...
0
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...
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
marktang
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,...
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
jinu1996
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...
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,...

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.