c++单例模式

(c++11中,局部静态变量是线程安全的)

把下面 C++ 代码片段中的 Singleton 替换成实际类名, 快速得到一个单例类:

class Singleton {
public:
    static Singleton& Instance() {
        static Singleton theSingleton;
        return theSingleton;
    }

    /* more (non-static) functions here */

private:
    Singleton();                            // ctor hidden
    Singleton(Singleton const&);            // copy ctor hidden
    Singleton& operator=(Singleton const&); // assign op. hidden
    ~Singleton();                           // dtor hidden
};

构造函数,析构函数,拷贝函数和赋值函数都设为private类型
posted @ 2016-06-21 14:42  叫我二当家  阅读(66)  评论(0)    收藏  举报