C++ singleton vs. global static object

前端 未结 8 1415
孤街浪徒
孤街浪徒 2020-11-28 20:06

A friend of mine today asked me why should he prefer use of singleton over global static object? The way I started it to explain was that the singleton can have state vs. s

相关标签:
8条回答
  • 2020-11-28 20:25

    In C++, the order of instantiation of static objects in different compilation units is undefined. Thus it's possible for one global to reference another which is not constructed, blowing up your program. The singleton pattern removes this problem by tying construction to a static member function or free function.

    There's a decent summary here.

    0 讨论(0)
  • 2020-11-28 20:27

    OK, there are two reasons to go with a singleton really. One is the static order thing everyone's talking about.

    The other is to prevent someone from doing something like this when using your code:

    CoolThing blah;
    gs_coolGlobalStaticThing = blah;
    

    or, even worse:

    gs_coolGlobalStaticThing = {};
    

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

    0 讨论(0)
  • 2020-11-28 20:34

    In C++, there's not a huge amount of difference between the two in terms of actual usefulness. A global object can of course maintain its own state (possibly with other global variables, though I don't recommend it). If you're going to use a global or a singleton (and there are many reasons not to), the biggest reason to use a singleton over a global object is that with a singleton, you can have dynamic polymorphism by having several classes inheriting from a singleton base class.

    0 讨论(0)
  • 2020-11-28 20:35

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

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

    This is technically a singleton though, this function can even be a static method of a class. So it guaranties to be constructed before used unlike with global static objects, that can be created in any order, making it possible to fail unconsistently when one global object uses another, quite a common scenario.

    What makes it better than common way of doing singletons with creating new instance by calling new is that object destructor will be called at the end of a program. It won't happen with dynamically allocated singleton.

    Another positive side is there's no way to access singleton before it gets created, even from other static methods or from subclasses. Saves you some debugging time.

    0 讨论(0)
  • 2020-11-28 20:40

    Another benefit of the Singleton over the global static object is that because the constructor is private, there is a very clear, compiler enforced directive saying "There can only be one".

    In comparison, with the global static object, there will be nothing stopping a developer writing code that creates an additional instance of this object.

    The benefit of the extra constraint is that you have a guarantee as to how the object will be used.

    0 讨论(0)
  • 2020-11-28 20:43

    Using Singleton("construct on first use") idiom, you can avoid static initialization order fiasco

    0 讨论(0)
提交回复
热议问题