By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 818 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

counting objects

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.