Static singleton and Q_GLOBAL_STATIC
-
I'm trying to use this "macro ":http://lists.trolltech.com/qt-interest/2008-03/msg00022.html to define a static singleton of one class.
Here is what I wrote in MyClass.h
@class MyClass : public QObject
{
Q_OBJECTpublic:
MyClass();
void doSomething();
};Q_GLOBAL_STATIC(MyClass, myClassInstance)@
But then it creates two different instances when I use myClassInstance()->doSomething() in my mainwindow, and once more in a QDialog created from the mainwindow (same thread)
What's wrong? -
Hum, I changed it to:
MyClass.h
@class MyClass : public QObject
{
Q_OBJECTpublic:
MyClass();
static MyClass* instance();
void doSomething();
};@
and MyClass.cpp
@Q_GLOBAL_STATIC(MyClass, myClassInstance)MyClass *MyClass::instance()
{
return myClassInstance();
}@
Now, am I doing it right? -
Dear Volker, considering that myClassInstance() is defined in the .cpp file, what method will you call if you remove the instance() one?
-
Ok thanks for the clarification
-
Yes it does.