counting objects

A

Agent Mulder

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
 
V

Victor Bazarov

Agent Mulder said:
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
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

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.
 
A

Agent Mulder

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
 
A

Andrew Heath

Agent said:
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
 
A

Agent Mulder

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
 
K

Kristoffer Vinther

Agent Mulder said:
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
 

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
474,141
Messages
2,570,813
Members
47,357
Latest member
sitele8746

Latest Threads

Top