W
wizwx
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.
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.