Suppose we have a class like
class Egg
{
static Egg e;
int i;
Egg(int ii):i(ii) {}
Egg(const Egg &);
public:
static Egg* instance() {return &e}
};
Egg Egg::e(47);
This code ensures that we cannot create any object, but we can only use a static object. But how can we declare a static object of the same class in the class.
And one more thing, since e is a static object, and static objects can only call static member functions, therefore, as a constructor, it can be called here for a static object e, and its constructors are private.
source
share