0

I'm learning how to implement the singleton with C++ and I've read this link: C++ Singleton design pattern

I have one question: Why is the "Meyers Singleton" guaranteed-destruction?

class S
{
    public:
        static S& getInstance()
        {
            static S    instance;
            return instance;
        }
    private:
        S() {}
        S(S const&);  
        void operator=(S const&);
 };

In my opinion, in this case, the time of destruction of the variable instance is out of control too. Meaning that it is destroyed after the main returns. If I'm right , why it can be regarded as guaranteed-destruction?

class S
{
    public:
        static S& getInstance()
        {
            return instance;
        }
        S() {}
    private:
        static S instance;
        S(S const&);  
        void operator=(S const&);
 };

 S S::instance = S();

Is this guaranteed-destruction too?

Yves
  • 11,597
  • 17
  • 83
  • 180

0 Answers0