Is Q_GLOBAL_STATIC recommended to use?
-
As far as I know, Q_GLOBAL_STATIC provide a convinient way to define a
singleton
like instance, I quoted it because I often prefer to use a global static instance as a singleton and get the pointer of the instance of a class by defining a getInstatnce() function, however if we dont't make ctor as private, we still can create many instances. This may be confused for use this class.So, how can I correctly use Q_GLOBAL_STATIC ?
-
Hi @aiyolo,
Q_GLOBAL_STATIC
provide a convenient way to define asingleton
like instanceI'd say that's is not the right way to see
Q_GLOBAL_STATIC
. You'll note that the docs do not mention "single" or "singleton" anywhere. As @sierdzio suggested, this is about convenient global access, whereas a singleton (which is not necessarily globally accessible) is about enforcement of an only-one-instance policy.Consider, for example, a database connection object. It's often easy (but wrong) to assume that "just one" db connection is ideal for an app, and so use a singleton. However, its often much more useful to have at least two db connections - one for reading, and one for writing, so that they can connect to different read/write instances in a cluster. So in this scenario, its very helpful to have global access to a couple of static-like instances of the single class, but not to be enforcing a single instance of it (so not a singleton).
Is Q_GLOBAL_STATIC recommended to use?
In my opinion, yes, but for non-POD static objects, not for the singleton pattern.
I also recommend reading C++ Core Guidelines - Section I.3 Avoid singletons.
Cheers.
-
@aiyolo said in Is Q_GLOBAL_STATIC recommended to use?:
So, how can I correctly use Q_GLOBAL_STATIC ?
Well, use it whenever you have some piece of code you only want to create once but use many times. Think of it like a static constant value.
-
Hi @aiyolo,
Q_GLOBAL_STATIC
provide a convenient way to define asingleton
like instanceI'd say that's is not the right way to see
Q_GLOBAL_STATIC
. You'll note that the docs do not mention "single" or "singleton" anywhere. As @sierdzio suggested, this is about convenient global access, whereas a singleton (which is not necessarily globally accessible) is about enforcement of an only-one-instance policy.Consider, for example, a database connection object. It's often easy (but wrong) to assume that "just one" db connection is ideal for an app, and so use a singleton. However, its often much more useful to have at least two db connections - one for reading, and one for writing, so that they can connect to different read/write instances in a cluster. So in this scenario, its very helpful to have global access to a couple of static-like instances of the single class, but not to be enforcing a single instance of it (so not a singleton).
Is Q_GLOBAL_STATIC recommended to use?
In my opinion, yes, but for non-POD static objects, not for the singleton pattern.
I also recommend reading C++ Core Guidelines - Section I.3 Avoid singletons.
Cheers.
-