为什么Singleton是比全局对象更好的选择

这个问题在这里已经有了答案:

  • 单身人士有什么不好? [已关闭] 36个回答
  • C ++单例与全局静态对象8个答案

  • 他们是懒惰的构造:

    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/82163.html

    上一篇: Why is Singleton a better choice than Global Objects

    下一篇: Why is using a Global Variables class (singleton) bad practise?