标签:
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; class Singleton { public: int i; static Singleton* GetInstance(); virtual void Show() {} protected: //必须为保护,如果是私有属性,子类无法访问父类的构造函数 Singleton() {} private: static Singleton *singleton; //唯一实例的指针 }; Singleton* Singleton::singleton = NULL; Singleton* Singleton::GetInstance() { if(singleton == NULL) { if(singleton == NULL) singleton = new Singleton(); } return singleton; } int main() { Singleton* sin=Singleton::GetInstance(); sin->i=200; Singleton* sin2=Singleton::GetInstance(); cout<<sin2->i<<endl; return 0; }
标签:
原文地址:http://www.cnblogs.com/yufenghou/p/4259310.html