我刚刚进入C ++中的创作设计模式,我选择从Singleton类开始。我经历了许多在线教程,发现单身人士可以为我做的是:
我的问题是:
以下是我的代码。请帮我理解。
#include<iostream>
using namespace std;
class hello
{
public:
static hello *OneObj;
static hello *MakeOneObject(); //funtn with return static class obj
private:hello()
{
cout<<"One Object created"<<endl;
}
~hello()
{
cout<<"Object deleted"<<endl;
}
};
hello* hello::OneObj=NULL;
hello* hello::MakeOneObject()
{
if(OneObj==NULL){
cout<<"in if"<<endl;
// hello OneObj; why stack object not possible?
OneObj=new hello();
}
return OneObj;
}
int main()
{
hello *h,*jj;
h=hello::MakeOneObject();// created
jj=hello::MakeOneObject(); // not created
}