简单的C++单例模式




#ifndef SINGLETON_H
#define SINGLETON_H
class CSingleton
{
    private:
    CSingleton()  //构造函数是私有的
    {
    }
    static CSingleton *m_pInstance;
    public:
    static CSingleton * GetInstance()
    {
        if(m_pInstance == NULL)  //判断是否第一次调用
        m_pInstance = new CSingleton();
        return m_pInstance;
    } 
    charstr[32];//属性
};
CSingleton* CSingleton::m_pInstance=NULL;//这句必须加,一般来说,要加在cpp文件中。不然会出现“unresolved external symbol”的错误。
#endif


猜你喜欢

转载自blog.csdn.net/sspdfn/article/details/52700304