Singleton pattern

Hi,

I have a question regarding the Singleton design pattern. In a code like this:

class Singleton {
static Singleton s;
Singleton (int) {}
Singleton (const Singleton &);
public:
static Singleton *instance() { return &s; }
};

Singleton Singleton::s (3);

Why am I allowed to access the private constructor in the definition of s? I guess it's because s is a member variable of Singleton? Because clearly, a definition like

Singleton s (3);

would not work since the constructor is private. However, if I defined a class member of type X (where X is a different class) within the Singleton class, that class member would not have access to the private members of Singleton. So I guess the Singleton pattern works because I have a member variable of Singleton that is itself a Singleton, and as such has access to the private constructor of Singleton?

I am confused :-(

Many thanks for your help,
Shen

I guess it's because s is a member variable of Singleton?


^ This

As for the rest, you're over thinking.

It's as simple as this... Anything inside the class can access private members.

Your 's' object is inside the Singleton class, therefore it can access the private ctor.

Anything else you do can access the private ctor as long as it's inside of the Singleton class.
Thanks a lot!
Topic archived. No new replies allowed.