In this example, I expected that the instance was re-initialzed to NULL everytime. Thus, it shouldn't have worked, it should do new everytime. But, it works actually as singleton. Thus,the new is called only once. Why it works? I am confused here.
class Factory_model
{
public:
static Factory_model* Instance(void);
};
Factory_model* Factory_model::Instance(void)
{
static Factory_model* instance = NULL;
if(instance == NULL)
{
qDebug()<< "Creating instance now"<<endl;
instance = new Factory_model;
}
return(instance);
}
int main(int argc, char *argv[])
{
Factory_model *ptr = Factory_model::Instance();
Factory_model *ptr2 = Factory_model::Instance();
Factory_model *ptr3 = Factory_model::Instance();
}
The output is the following - Creating instance now