Created
February 10, 2012 00:54
-
-
Save arekinath/1784901 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "singleton.h" | |
#include <QCoreApplication> | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication ca(argc, argv); | |
Singleton::instance()->init(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "singleton.h" | |
Singleton::Singleton() : QObject(NULL) | |
{ | |
} | |
template <typename T> | |
class MyGlobalStatic | |
{ | |
public: | |
T *pointer; | |
inline MyGlobalStatic(T *p) : pointer(p) { } | |
inline ~MyGlobalStatic() { pointer = 0; } | |
}; | |
Singleton *Singleton::instance() | |
{ | |
static Singleton s; | |
static MyGlobalStatic<Singleton> st(&s); | |
return st.pointer; | |
} | |
Singleton::~Singleton() | |
{ | |
} | |
void Singleton::init() | |
{ | |
Singleton *s = Singleton::instance(); | |
const QMetaObject *mo = s->metaObject(); | |
qDebug(mo->className()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QObject> | |
class Singleton : public QObject | |
{ | |
Q_OBJECT | |
public: | |
~Singleton(); | |
static Singleton *instance(); | |
void init(); | |
private: | |
explicit Singleton(); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TEMPLATE = app | |
SOURCES += main.cpp singleton.cpp | |
HEADERS += singleton.h | |
QT = core |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment