C++ (Qt)class Example : public QObject{ Q_OBJECTpublic: explicit Example(QObject *parent = 0); static Example* instanse(); //... private: static Example* _instanse; //...};
C++ (Qt)Example* Example::_instanse = 0; Example::Example(QObject *parent) : QObject(parent){ if (!_instanse) _instanse = this; //...} Example *Example::instanse(){ return _instanse;}
C++ (Qt) new Example(this);
C++ (Qt) Example* ex = Example::instanse();
C++ (Qt)while(!asleep()) sheep++;
class OnlyOne{public: static OnlyOne &Instance() { static OnlyOne theSingleInstance; return theSingleInstance; }private: OnlyOne() {} OnlyOne(const OnlyOne&); OnlyOne &operator=(const OnlyOne&);};
OnlyOne &s = OnlyOne::Instance;
class MapObject{public: static MapObject *getInstance() { if(!instance) instance = new MapObject(); return instance; } ~MapObject();private: static MapObject *instance; MapObject();}
static T& instance() { Q_ASSERT_X(s_pInstance, "instancing", "The singleton has not yet been created."); return (*s_pInstance); }private: // Data... //---------------------------------------------------------------------- static T* s_pInstance;