Managing a singleton destructor

后端 未结 6 1733
眼角桃花
眼角桃花 2020-12-18 04:08

The following small example implements a singleton pattern that I\'ve seen many times:

#include 

class SingletonTest {
private:
  Singleton         


        
6条回答
  •  执笔经年
    2020-12-18 04:38

    ...not exactly a direct answer, but too long for a comment - why not do the singleton this way:

    class SingletonTest {
    private:
      SingletonTest() {}
      ~SingletonTest() {
        std::cout << "Destructing!!" << std::endl;
      }
    
    public:
      static SingletonTest& get_instance()  {
        static SingletonTest instance;
        return instance;
      }
    };
    

    Now you have a lazy singleton that will be destructed on exit... It's not any less re-entrant than your code...

提交回复
热议问题