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

前端 未结 2 980
后悔当初
后悔当初 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 03:44

    A common way of accomplishing this is with a class template from which you inherit.

    template <typename T>
    class Countable
    {
        static unsigned cs_count_;
    public:
        Countable() { ++cs_count_; }
        Countable( Countable const& ) { ++cs_count_; }
        virtual ~Countable() { --cs_count_; }
        static unsigned count() { return cs_count_; }
    };
    
    template <typename T>
    unsigned Countable<T>::cs_count_ = 0;
    

    So to use this I would write:

    class MyClass : public Countable<MyClass> { };
    

    Below is a thread-safe version. It uses a class from boost to ensure the increment, decrement, and read operations are atomic on the supported platforms.

    #include <boost/detail/atomic_count.hpp>
    
    template <typename T>
    class Countable
    {
        static boost::detail::atomic_count cs_count_;
    protected:
        ~Countable() { --cs_count_; }
    public:
        Countable() { ++cs_count_; }
        Countable( Countable const& ) { ++cs_count_; }
        static unsigned count() { return cs_count_; }
    };
    
    template <typename T>
    boost::detail::atomic_count Countable<T>::cs_count_(0);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题