I've a C++ snippet as below. The "getInstance()" function is trying to return a static object of the same class "CAbc". This class has a private constructor so that objects of this class can't be created.
While calling this function in main(), like in below snippet, the returned object is not being collected anywhere in main() using any reference.
I'm trying to understand the below 2 points:
What is this way of creating a static object & returning it from within the static member function of same class type ? There is no object created when a constructor is private. Then how does this object return work here ?
And how does this way of not collecting the object in main() work ?
This is being called in the main() function.
class CAbc
{
private:
CAbc() {} // HAS A PRIVATE CONSTRUCTOR
.....
public:
static CAbc& getInstance()
{
static CAbc _self;
return _self;
}
// what does this returning a static self object mean in C++ ?
}
main()
{
CAbc::getInstance();
// not collectig this anywhere ?
}