<pre name="code" class="cpp">/*Singleton.h*/ #ifndef SINGLETON_H #define SINGLETON_H class Singleton { public: static Singleton *Instance(); protected: Singleton(); private: static Singleton *instance_; }; #endif
<pre name="code" class="cpp">/*Singleton.cpp*/ #include "Singleton.h" #include <iostream> Singleton *Singleton::instance_=0; Singleton::Singleton() { std::cout<<"Singleton..."<<std::endl; } Singleton *Singleton::Instance() { if(instance_==0) { instance_=new Singleton(); } return instance_; }
/*main.cpp*/ #include "Singleton.h" int main() { Singleton *sgn=Singleton::Instance(); return 0; }
时间: 2024-10-17 19:59:01