vendredi 5 mars 2021

C++11 singlton with static instance - returning pointer

So here's C++11 Singleton that is lazy-evaluated, correctly-destroyed, and thread-safe.  (c)

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

Is it ok or are there any downsides if to write getInstance like this?

static S* getInstance()
            {
                static S    instance; 
                return &instance;
            }

Aucun commentaire:

Enregistrer un commentaire