标签:
单例模式:确保一个类只有一个实例,并提供全局访问点。——《HEAD FIRST 设计模式》
我的c++代码:
#ifndef DESIGN_SINGLETON_H_ #define DESIGN_SINGLETON_H_ #include <iostream> class Singleton { private: Singleton(){} public: static Singleton* GetInstance(); private: static Singleton* g_instance; }; #endif // DESIGN_SINGLETON_H_
#include "singleton.h" Singleton* Singleton::g_instance = new Singleton(); Singleton* Singleton::GetInstance() { if (g_instance == 0) { std::cout << "singleton initing..." << std::endl; g_instance = new Singleton(); } return g_instance; }
标签:
原文地址:http://www.cnblogs.com/foolbread/p/4459741.html