看了SAP的笔试题目,顺手写一个单件模式

来源:互联网 发布:海康网络摄像机保存 编辑:程序博客网 时间:2024/05/01 05:48

                   看了SAP的笔试题目,顺手写了个单件模式

class singleton
{
public:
        static singleton* getinstance()
        {
                if(m_data==0)
                return new singleton();
                return m_data;
        }
protected:
        singleton(){}
private:
          static singleton* m_data;
};
singleton* singleton::m_data=0;
int main(int argc, char* argv[])
{
        singleton* single=singleton::getinstance();
        if(single)
        cout<<"ok!"<<endl;
        else
        cout<<"error!"<<endl;

        system("pause");
        return 0;

原创粉丝点击