c++ - How can I safely (and easily) count *all* instances of a class within my program? -


I would like to be able to start a special (and otherwise normal) class (the source I can modify) And the count count has been repeatedly (for example). But I would like to include all the examples in my total count, even in the standard container, anything created through the copy constructor.

Is it all the constraint for me (copy creator) class to rise to a stable counter? If so, is it possible to ensure that my class is still consistent with the requirements of the standard container (i.e., t x) x, for example by overriding other operators? Think of the static class variable as a global variable that is in the namespace of the class.

Increasing the other things along with it will not have any side effects on other codes, meaning that your constructor and other operators will behave in the same way.

Ie, you are right: increase in all the constructors and decrease in them.


Of course, as George said, if you want to secure it multithreading, you have to add some multithrading secure code around access to your counter variable (for example some mute X). Or as Steven pointed out, you can also use nuclear increment / decreasing instructions (but usage will depend on platform). It will be very fast, however, you have to be careful because in some cases that do not work in a multi-processor environment, you can use the atomic_count boost for the safer side.


Comments