I have a singleton:
struct foo {
static foo& instance() {
static foo f;
return f;
}
};
When re-arranging some code I ended up with this statement “by error”:
foo::foo::instance()
But this is deemed correct by my compiler (gcc 4.7). In fact, even foo::foo::foo::instance()
compiles.
Why?
1 Like
Static members are shared by all instances of a class and their lifetime is lifetime of the program. Thus static members are accessed using scope operator as static members do not belong to any particular object. If you deny this then this may create an illusion for the object that foo is its member which is not what we want.
Hence it was standardized to access static members using scope operator.
Hope it helps
1 Like