JJ>>> class foo { private:
JJ>>> static int HowMany ;
JJ>>> public:
JJ>>> foo() { ++HowMany ; }
JJ>>> ~foo() { --HowMany ; }
JJ>>> } ;
JD>> This scheme doesn't work unless you provide a copy constructor as
JD>> well.
CC> What about the fact that HowMany is not initialized and can be
CC> any number at runtime?
That's not the case.
If foo::HowMany were not *defined* (as it isn't in the above) then the
program would not link. I was generously assuming that foo::HowMany is
defined somewhere, since that is just nitpicking.
However, even if foo::HowMany were defined without an initialiser
static int foo::HowMany ;
then it would be zero-initialised. Objects with static storage duration are
guaranteed to be statically initialised to zero at program startup in C++.
It is only objects with heap storage duration or with automatic storage
duration where one has the problem of the "indeterminate value" if one does
not use an initialiser.
¯ JdeBP ®
--- FleetStreet 1.19 NR
---------------
* Origin: JdeBP's point, using Squish (2:440/4.3)
|