#include <iostream> #include <string> using namespace std; class Singleton { public: static Singleton* GetInstance(); void print(); void write(string strTmp); private: Singleton(); static Singleton* instance; string str; }; Singleton::Singleton() { str="Test"; } Singleton* Singleton::GetInstance() { if (instance==NULL) instance=new Singleton(); return instance; } void Singleton::print() { cout<<str<<endl; } void Singleton::write(string strTmp) { str=strTmp; } Singleton * Singleton::instance = NULL; void main() { Singleton* p=Singleton::GetInstance(); Singleton* p2=Singleton::GetInstance(); p->print(); p2->print(); p->write("haha"); p->print(); p2->print(); }