单例模式

it2024-11-21  1

#include <iostream> using namespace std; class Singleton { private: Singleton() { } ~Singleton() { } static Singleton* pSingleton; public: static Singleton* GetInstance() { if (pSingleton == NULL) { pSingleton = new Singleton(); } return pSingleton; } }; Singleton* Singleton::pSingleton; int main() { Singleton* p1 = Singleton::GetInstance(); cout<<hex<<p1<<endl; Singleton* p2 = Singleton::GetInstance(); cout<<hex<<p2<<endl; return 0; }

转载于:https://www.cnblogs.com/yanjiu/archive/2012/08/20/2647987.html

相关资源:数据结构—成绩单生成器
最新回复(0)