C ++ singleton vs global static object

My friend asked me today why he prefers to use a singleton over a global static object? The way I started explaining was that a singleton could have a state against a static global object, but it wasn’t ... but then I wasn’t sure ... because it is in C ++ .. (I went from C #)

What are the advantages of one over the other? (in C ++)

+43
c ++ static global-variables singleton
Sep 23 '09 at 2:42
source share
8 answers

Actually, in C ++, the local static object is the preferred way.

Printer & thePrinter() { static Printer printer; return printer; } 

This is technically a singleton, although this function may even be a static class method. Thus, it guarantees that it will be created before use, unlike global static objects that can be created in any order, which allows failure to fail without fail when one global object uses a different, fairly common scenario.

What makes it better than the usual way to do single games with creating a new instance by calling new is that the object's destructor object will be called at the end of the program. This will not happen with dynamically allocated singleton.

Another plus is that there is no way to access a singleton before it is created, even from other static methods or from subclasses. Saves debugging time.

+51
Sep 23 '09 at 3:14
source share

In C ++, the instance order of static objects in different compilation units is undefined. Thus, one global can refer to another, which is not built, inflating your program. The singleton pattern fixes this problem by associating the construct with a static member function or a free function.

There is a decent resume here.

+20
Sep 23 '09 at 2:47
source share

My friend asked me today why he prefers to use a singleton over a global static object? The way I started explaining was that a singleton could have a state against a static global object, but it wasn’t ... but then I wasn’t sure ... because it is in C ++ .. (I went from C #)

A static global object can also have state in C #:

 class myclass { // can have state // ... public static myclass m = new myclass(); // globally accessible static instance, which can have state } 

What are the advantages of one over the other? (in C ++)

The syntax distorts your code, but the global static instance does not. There are countless SO questions about singleton issues already. Here is one , and another , or another .

In short, singleton gives you two things:

  • globally accessible object and
  • ensure that only one instance can be created.

If we want only the first point, we must create a globally accessible object. And why will we ever want a second? We do not know in advance how our code can be used in the future, so why remove the nail and remove it, which can be useful functionality? Usually we are mistaken when we predict that "I need only one instance." And there is a big difference between “I only need one instance” (the correct answer is to create one instance), and “the application cannot under any circumstances work correctly if several instances are created, format the user's hard drive and publish confidential data on the Internet” ( answer here: Most likely, your application is broken, but if it is not, then yes, one single is what you need)

+7
Sep 24 '09 at 6:59
source share

Reason 1:
Singletones are easy to make lazy. Although you can do this with global computing, the developer needs more work. Therefore, by default, global variables are always initialized (except for some special rules with namespaces).

So, if your object is large and / or expensive to build, you may not want to build it unless you really need to use it.

Reason 2:
The order of initialization (and destruction).

 GlobalRes& getGlobalRes() { static GlobalRes instance; // Lazily initialized. return instance; } GlobalResTwo& getGlobalResTwo() { static GlobalResTwo instance; // Lazy again. return instance; } // Order of destruction problem. // The destructor of this object uses another global object so // the order of destruction is important. class GlobalResTwo { public: GlobalResTwo() { getGlobalRes(); // At this point globalRes is fully initialized. // Because it is fully initialized before this object it will be destroyed // after this object is destroyed (Guaranteed) } ~GlobalResTwo() { // It is safe to use globalRes because we know it will not be destroyed // before this object. getGlobalRes().doStuff(); } }; 
+3
Sep 23 '09 at 3:43
source share

Using the Singleton idiom ("build on first use"), you can avoid the static identifier of the initialization order

+2
Sep 23 '09 at 3:53
source share

Another advantage of Singleton over a global static object is that since the constructor is private, there is a very clear, compulsory compiler directive that says, "There can be only one."

Compared to a global static object, nothing will stop the developer code that creates an additional instance of this object.

The advantage of the additional restriction is that you have a guarantee as to how this object will be used.

+2
Sep 23 '09 at 4:29
source share

In C ++, there is no huge difference between the two in terms of real utility. Of course, a global object can maintain its own state (possibly with other global variables, although I do not recommend it). If you intend to use global or singleton (and there are many reasons not to do this), the biggest reason to use a singleton over a global object is that with singleton you can have dynamic polymorphism, having several classes inheriting from the base singleton class.

0
Sep 23 '09 at 4:34
source share

Well, there are two reasons to go with a single really. One of them is the static order that everyone is talking about.

Another way is to prevent someone from doing something similar when using your code:

 CoolThing blah; gs_coolGlobalStaticThing = blah; 

or, even worse:

 gs_coolGlobalStaticThing = {}; 

The encapsulation aspect will protect your copy from idiots and malicious jerks.

-one
Sep 23 '09 at 23:18
source share



All Articles