Why is Singleton a better choice than Global Objects

This question already has an answer here:

  • What is so bad about singletons? [closed] 36 answers
  • C++ singleton vs. global static object 8 answers

  • 他们是懒惰的构造:

    LargeObject   global;                    // Large object is always constructed.
    
    class LargeObject_Singelton
    {
        public: static LargeObject& getInstance()
        {
            static LargeObject  instance;     // This object is not created until the first
            return instance;                  // time getInstance() is called.
        }                                     // If you never use it this can be important.
    };
    
    链接地址: http://www.djcxy.com/p/82164.html

    上一篇: 实现接口的单例类

    下一篇: 为什么Singleton是比全局对象更好的选择