C++ 单例模式

来源:互联网 发布:软件文件夹命名规则 编辑:程序博客网 时间:2024/04/29 16:57

思路:构造函数私有,对外仅提供一个公开的函数来获取该实例。
同时考虑到析构的时候。

class CSingleton{    public:        static CSingleton getInstance()        {            if(m_pInstance == null)            {                m_pInstance = new CSingleton();            }            return m_pInstance;        }    private:        CSingleton();        static CSingleton * m_pInstance;        class CGarbo //when CGarbo destructed, it will delte CSingleton::m_pInstance        {            public:                ~CGarbo()                {                    if(CSingleton::m_pInstance != NULL)                    {                        delete CSingleton::m_pInstance;                    }                }        }        static CGarbo m_garbo;}

博主Github:https://github.com/linmq 欢迎访问。

参考资料:http://blog.csdn.net/jackystudio/article/details/11764493
http://blog.csdn.net/chenyufeng1991/article/details/47925457
http://blog.csdn.net/crayondeng/article/details/24853471

0 0
原创粉丝点击