1. 单例模式的类只提供私有的构造函数
  2. 类定义中含有一个该类的静态私有对象
  3. 该类提供了一个静态的公有的函数用于创建或获取它本身的静态私有对象
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Singleton{
public:
static Singleton* getInstance(){
	if (instance_ == NULL) {
		instance_ = new Singleton();
	}
	return instance_;
}
private:
	Singleton(){}
	Singleton(const Singleton&){}
	Singleton& operator = (const Singleton&){}
	static Singleton* instance_;
};

Singleton* Singleton::instance_ = NULL;

Reference

参考资料