Can anyone please advise what does following code mean? It looks like singleton pattern but uses & instead of pointer.
class Class
{
public:
static Class &Function();
}
Class &Class::Function()
{
static Class class1;
return class1;
}
Can anyone please advise what does following code mean? It looks like singleton pattern but uses & instead of pointer.
class Class
{
public:
static Class &Function();
}
Class &Class::Function()
{
static Class class1;
return class1;
}
Singleton is an idea made by programmers. It means that it holds no specified form behind it (especially dealing with c++ pointers and references) and only has it's set of rules. The code you see infront of you meant to create a static variable when the function is called and keep it till the end of the whole program comes. Returning it by pointer of reference doesn't matter. It just holds a specified single object which won't be destroyed after the end of the scope (because of the static declaration) and will return only that object. Of course you may just put it as a private variable and return it whenever somebody is going to call the function. But, it always depends on the user it self. Pay your attention to this question: Difference between Singleton implemention using pointer and using static object