By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,847 Members | 2,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,847 IT Pros & Developers. It's quick & easy.

A question regarding to the keyword static

P: n/a
I just noticed an interesting implementation of Singleton from Bruce
Eckel's "Thinking in C++" vol. 2, pp 620:
class Singleton {
static Singleton s;
public:
static Singleton& instance() {
return s;
}
// ....
};
Singleton Singleton::s;

It is interesting to note that a static member of itself (Singleton)
is declared inside the Singleton class. I tested the code in linux,
and g++ compiled it successfully.

My question is: How could this happen? How could a class ever have a
member of the type of itself? This is obviously invalid if there is no
keyword static. However, what features of the keyword "static" make
this feasible?

Thanks for any comments or inputs.

Mar 22 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a

wizwx schrieb:
I just noticed an interesting implementation of Singleton from Bruce
Eckel's "Thinking in C++" vol. 2, pp 620:
class Singleton {
static Singleton s;
public:
static Singleton& instance() {
return s;
}
// ....
};
Singleton Singleton::s;

It is interesting to note that a static member of itself (Singleton)
is declared inside the Singleton class. I tested the code in linux,
and g++ compiled it successfully.

My question is: How could this happen? How could a class ever have a
member of the type of itself? This is obviously invalid if there is no
keyword static. However, what features of the keyword "static" make
this feasible?

Thanks for any comments or inputs.
A static class is only instanciated once. A normal member would result
a infinite class size and recursive constructor call.

Mar 22 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.