C++ 单例模式

来源:互联网 发布:怎样去掉mac上win 编辑:程序博客网 时间:2024/06/06 05:10

代码块

头文件

 #include <iostream>class BrowserSetting{public :    static BrowserSetting *GetInstance()    {        if (m_Instance == NULL)        {            m_Instance = new BrowserSetting();        }        return m_Instance;    }    static void DestoryInstance()    {        if (m_Instance != NULL)        {            delete m_Instance;            m_Instance = NULL;        }    }    // This is just a operation example    int GetWidth()    {        return m_Width;    }    int GetHeight()    {        return m_Height;    }    void ChangeWidth(int width)    {        m_Width = width;    }    void ChangeHeight(int height)    {        m_Height = height;    }private:    BrowserSetting()     {         m_Width = 1360;         m_Height = 768;    }    static BrowserSetting *m_Instance;    int m_Width , m_Height;};

引用

BrowserSetting *BrowserSetting::m_Instance = NULL;BrowserSetting *b = BrowserSetting::GetInstance();
原创粉丝点击