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类型

浙公网安备 33010602011771号