Static member object of a class in the same class

Posted by Luv on Stack Overflow See other posts from Stack Overflow or by Luv
Published on 2012-06-03T22:11:01Z Indexed on 2012/06/03 22:40 UTC
Read the original article Hit count: 205

Filed under:
|
|

Suppose we have a class as

class Egg
{
static Egg e;
int i;
Egg(int ii):i(ii) {}
Egg(const Egg &);    //Prevents copy-constructor to be called
public:
static Egg* instance() {return &e}
};

Egg Egg::e(47);

This code guarantees that we cannot create any object, but could use only the static object. But how could we declare static object of the same class in the class.

And also one thing more since e is a static object, and static objects can call only static member functions, so how could the constructor been called here for static object e, also its constructors are private.

© Stack Overflow or respective owner

Related posts about c++

Related posts about static