> One obvious mistake in this solution is that it does not take into account
copies of objects being made.
Scott Meyers has given a good solution -
http://www.cuj.com/documents/s=8066/cuj9804meyers/
In the article Scott is exploring various possibilities of implementing an
instance counter. Finally he finds out that the best one is to use curiously
recurring template pattern and private inheritance. The only unsolved
problem is that howMany() member function is not accessible, and must be
made so with an explicit using declaration inside the class body.
One thing I do not understand is why does he not use a static member
function howMany() from Counter class template directly, but insists on
having a public howMany() member in all derived classes?
It's use might look like that:
Counter<Widget>::getCount();
instead of
Widget::howMany();
Best regards,
Marcin