看了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;
}