Referring to the following:
class A { ... };
class B
{
static A a; // this fails
...
static A& getA() { static A a; return a; } // this works
...
};
...
B b;
b.a <-- gives error: undefined reference to B::a
Why can I not have a static A
in class B
, but it is fine to return it from a method?
[edit]
Just something curious:
struct C
{
static const int x = 5;
};
int main()
{
int k = +C::x;
std::cout << "k = " << k << "\n";
return 0;
}
output: k = 5
C::x
is not defined in implementation-scope, neither is there an instance of C
, and yet, with the unary +
C::x
is accessible ... !?