Is there any difference between these methods, other than the obvious that first return an object and second returns the address where the value of myclass is stored?
I’m asking because I rarely see the first method, in many sources, even of big enterprises such as Aeonsoft I always see method 2 being used. However some programmers don’t like & with return, please enlighten me about this.
static myclass& getinstance()
{
static myclass a;
return a;
}
static myclass* getinstance()
{
static myclass a;
return &a;
}
EDIT
Oh guys my bad, I forgot the & on the first method
The first one makes no sense, because it returns a copy. The latter is a bit silly because there’s no need for a pointer.
The usual way for a singleton creation function is to return a reference: