C++: Retriving values of static const variables at a constructor of a static variable
- by gilbertc
I understand that the code below would result segmentation fault because at the cstr of A, B::SYMBOL was not initialized yet. But why?
In reality, A is an object that serves as a map that maps the SYMBOLs of classes like B to their respective IDs. C holds this map(A) static-ly such that it can provide the mapping as a class function.
The primary function of A is to serve as a map for C that initializes itself at startup. How should I be able to do that without segmentation fault, provided that I can still use B::ID and B::SYMBOL in the code (no #define pls)?
Thanks!
Gil.
class A
{
public:
A()
{
std::cout<<B::ID<<std::endl;
std::cout<<B::SYMBOL<<std::endl;
}
};
class B
{
public:
static const int ID;
static const std::string SYMBOL;
}
const int B::ID = 1;
const std::string B::SYMBOL = "B";
class C
{
public:
static A s_A;
};
A C::s_A;
int main(int c, char** p)
{
}