C++ different singleton implementations

前端 未结 5 1723
遇见更好的自我
遇见更好的自我 2020-12-21 16:16

I usually implement the singleton pattern this way :

class Singleton
{
    public:
        virtual ~Singleton() {}

        static Singleton& GetInstance         


        
5条回答
  •  情歌与酒
    2020-12-21 17:06

    One important difference between the two is that the creation of the instance in the second example is thread-safe.

    You're absolutely right though, the constructor should be private.

    Here's a related question: https://stackoverflow.com/a/10479084/1158895

提交回复
热议问题