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

前端 未结 2 986
后悔当初
后悔当初 2020-12-06 03:01

I would like to be able to instantiate a particular (and otherwise normal) class (the source of which I can modify) and count the number of times the class has been instanti

2条回答
  •  佛祖请我去吃肉
    2020-12-06 04:01

    Think of the static class variable as a global variable which is just in the namespace of the class. Incrementing or doing other things with it will not have any side effects on other code, i.e. your constructors and other operators will behave exactly as before.

    I.e., you are right: Just increment in all constructors and decrement in the destructor.


    Of course, as George pointed out, if you want to have it multithreading safe, you need to add some multithreading safe code around the access to your counter variable (for example some mutex). Or as Steven pointed out, you might also use atomic increment/decrement instructions (but the usage will depend on the platform). Those would be a lot faster. Though, you have to be careful because in some cases that wont work in multi-processor environments. You can use Boost's atomic_count to be on the safe side.

提交回复
热议问题