Q_GLOBAL_STATIC can't find created varibale after placing macro in cpp
-
I have a class that needs to have one object and accessed from multiple widgets (classes) in my project.
I was afraid singleton isn't thread-safe enough so I found the Q_GLOBAL_STATIC macro.
After adding the macro to the .h file of the class , I can use it from anywhere but seems that every time I inlclude the .h it basically creates another instance (and yes , I read that in documentation it says "More importantly, this macro should be placed in source files, never in headers. Since the resulting object is has static linkage, if the macro is placed in a header and included by multiple source files, the object will be defined multiple times and will not cause linking errors. ... "
However , when I add the macro to the .CPP other classes don't seem to find the variable :
'variable' was not declared in this scope. -
@Marioz said in Q_GLOBAL_STATIC can't find created varibale after placing macro in cpp:
Q_GLOBAL_STATIC
This is meant for global statics used in one translation unit, not for singletons distributed over the whole code. Singletons should be avoided and are never the correct solution for a problem.
I have a class that needs to have one object and accessed from multiple widgets (classes) in my project.
Then pass a pointer to this class to the other classes.
-
@Marioz said in Q_GLOBAL_STATIC can't find created varibale after placing macro in cpp:
Q_GLOBAL_STATIC
This is meant for global statics used in one translation unit, not for singletons distributed over the whole code. Singletons should be avoided and are never the correct solution for a problem.
I have a class that needs to have one object and accessed from multiple widgets (classes) in my project.
Then pass a pointer to this class to the other classes.
Then pass a pointer to this class to the other classes.
This was my very first approach but the problem is the huge number of widgets that are added.
An average session would require adding more than 30 widgets(instances) of 8 classes.
Is there any other way to make singleton-like approach without the risk of further troubles ? -
Why do you need a singleton in the first place? There is no need for it at all.
And creating a singleton in a cpp file and giving access to them from outside should be that hard for a c programmer...