Luv
Luv

Reputation: 5451

Static member object of a class in the same class

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.

Upvotes: 6

Views: 7975

Answers (2)

Daniel Hanrahan
Daniel Hanrahan

Reputation: 4969

But how could we declare static object of the same class in the class.

Normally you'd need a forward reference, but since Egg e is static, it's actually defined outside of the class definition. If e was not static, you'd get an error (something like "field Egg e has incomplete type").

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.

This is not quite true. A static member function within a class can only access static member data. static Egg e is an instance of Egg, so it can access all the members and data a regular Egg can.

also its constructors are private.

Any private member can be used from within a class. Since static Egg e is declared as a member of Egg, it can use the private constructor. The definition of e is outside the class since it's static, but it is still a class member.

And lastly your code doesn't compile because you left out a semicolon here:

static Egg* instance() {return &e;}

Upvotes: 2

Florian Sowade
Florian Sowade

Reputation: 1747

But how could we declare static object of the same class in the class.

A static member variable is not stored inside each object of a class. So if you declare a static member variable inside a class or as a namespace level object after you defined the class, differs only in respect to access (Class::var and var) and access to protected and private members.

And also one thing more since e is a static object, and static objects can call only static member functions

I think you are mixing static functions and static objects. Inside a static function you can call only static functions (unless you are calling them on an object).

so how could the constructor been called here for static object e

Like for every other object a constructor has to be called for static objects, too.

also its constructors are private

Access Control is checked on class level in C++. So since the static object is inside the class, it can access private members.

Unlike in some other languages, the following is legal in C++, since the access to a private member is from inside the class - even if on another object (other in this case):

 class Test {
 private:
      int i;
 public:
      Test(const Test &other)
      : i(other.i)
      {}
 };

Upvotes: 7

Related Questions