单例模式

来源:互联网 发布:2002数据库怎么使用 编辑:程序博客网 时间:2024/06/03 06:19
/*    单例模式*/class Single{private:    Single()    {}    static Single* pInstance;public:    static Single* getInstance()    {        if (pInstance == NULL)            pInstance = new Single();        return pInstance;    }};Single* Single::pInstance = NULL;/*    线程安全的几种方式    饿汉模式 无论是否使用在程序开始产生一个实例,并在以后返回    静态实例初始化 在进入主函数之前就已经完成初始化。*/class Singleton{private:    static const Singleton* m_instance;    Singleton() {}public:    static const Singleton* getInstance()    {        return m_instance;    }};const Singleton* Singleton::m_instance = new Singleton;//void test()//{//  const Singleton* s1 = Singleton::getInstance();//  const Singleton* s2 = Singleton::getInstance();////  if (s1 == s2)//      cout << "success" << endl;//}
3 0
原创粉丝点击