Skip to content

Instantly share code, notes, and snippets.

@arekinath
Created February 10, 2012 00:54
Show Gist options
  • Save arekinath/1784901 to your computer and use it in GitHub Desktop.
Save arekinath/1784901 to your computer and use it in GitHub Desktop.
#include "singleton.h"
#include <QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication ca(argc, argv);
Singleton::instance()->init();
return 0;
}
#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());
}
#include <QObject>
class Singleton : public QObject
{
Q_OBJECT
public:
~Singleton();
static Singleton *instance();
void init();
private:
explicit Singleton();
};
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