[设计模式]单实例

备忘 ^_^

.h 文件

Class OneWnd

{

private:

OneWnd(void);

~OneWnd(void);
static OneWnd* m_pInstance;

class CGarbo
    { 
    public: 
         ~CGarbo() 
         { 
             if (OneWnd::Instance())
             {
                 // to do clear something…

             }
         } 
    }; 
   
     static CGarbo Garbo;

public:

static OneWnd* Instance();

}

.cpp文件


OneWnd* OneWnd::m_pInstance = NULL;

OneWnd* OneWnd::Instance()
{
    if (m_pInstance == NULL )
    {
        m_pInstance = new OneWnd();
    }

    return m_pInstance;
}

以后不能忘了哈~~~

注:(2010-10-21)

Instance()方法里的 m_pInstance 这个值不能写成this->m_pInstance 会报错的哦

 


作者:GangWang
出处:http://www.cnblogs.com/GnagWang/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 
原文地址:https://www.cnblogs.com/GnagWang/p/1824444.html