Why C++ doesn't allow taking the address of a static data member when the data member is initialize within the class and doesn't have an out-of-class definition? How is the storage allocated for static member in this case?
The below minimum program demonstrate the issue.
#include <iostream>
class Test {
public:
static const int a = 99; // how is the storage allocated for Test::a??
};
// const int Test::a;
int main() {
std::cout << Test::a << '\n'; // OK, print 99
const int* ptr = &Test::a; // Linker error, undefined reference to Test::a
}
If I uncomment the line const int Test::a
, then the program works fine.