标签:alt 单例模式 ret tin getch ati singleton char 相同
1 class Singleton 2 { 3 private: 4 int data; 5 static Singleton *singletonInstance; 6 Singleton():data(0) 7 { 8 9 }; 10 Singleton(const Singleton &singleton) 11 { 12 data=singleton.data; 13 singletonInstance=singleton.singletonInstance; 14 }; 15 Singleton &operator=(const Singleton &m_operator) 16 { 17 data=m_operator.data; 18 singletonInstance=m_operator.singletonInstance; 19 return *this; 20 21 }; 22 ~Singleton(); 23 24 public: 25 static Singleton* getInstance() 26 { 27 if (singletonInstance!=NULL) 28 { 29 singletonInstance=new Singleton; 30 } 31 return singletonInstance; 32 } 33 }; 34 Singleton* Singleton::singletonInstance=NULL; 35 36 int main() 37 { 38 Singleton *m_singleton1=Singleton::getInstance(); 39 Singleton *m_singleton2=Singleton::getInstance(); 40 if (m_singleton1==m_singleton2) 41 { 42 cout<<"两个实例对象相同"<<endl; 43 } 44 else 45 { 46 cout<<"两个实例对象不相同"<<endl; 47 } 48 getchar(); 49 return 0;
输出结果:
标签:alt 单例模式 ret tin getch ati singleton char 相同
原文地址:https://www.cnblogs.com/wuhongjian/p/11569240.html