C++ singleton vs. global static object

前端 未结 8 1416
孤街浪徒
孤街浪徒 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:46

    Reason 1:
    Singletons are easy to make so they are lazy build.
    While you can do this with globals it take extra work by the developer. So by default globals are always initialized (apart from 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 have to use it.

    Reason 2:
    Order of initialization (and destruction) problem.

    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();
            }
    };
    
    0 讨论(0)
  • 2020-11-28 20:51

    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. static global object won't...but then I wasn't sure..because this in C++.. (I was coming from C#)

    A static global object can have state in C# as well:

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

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

    A singleton cripples your code, a global static instance does not. There are countless questions on SO about the problems with singletons already. Here's one, and another, or another.

    In short, a singleton gives you two things:

    • a globally accessible object, and
    • a guarantee that only one instance can be created.

    If we want just the first point, we should create a globally accessible object. And why would we ever want the second? We don't know in advance how our code may be used in the future, so why nail it down and remove what may be useful functionality? We're usually wrong when we predict that "I'll only need one instance". And there's a big difference between "I'll only need one instance" (correct answer is then to create one instance), and "the application can't under any circumstances run correctly if more than one instance is created. It will crash, format the user's harddrive and publish sensitive data on the internet" (the answer here is then: Most likely your app is broken, but if it isn't, then yes, a singleton is what you need)

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