Question: Is it possible to initialize a static const member of my class during run-time? This variable is a constant throughout my program but I want to send it as a command-line argument. 

```
//A.h
class A {
public: 
    static const int T;
};

//in main method
int main(int argc,char** argv)
{
    //how can I do something like 
    A::T = atoi(argv[1]);
}

```

If this cannot be done, what is the type of variable I should use? I need to initialize it at run-time as well as preserve the constant property.

Answer: 
You cannot rely on data produced after your `main` has started for initialization of `static` variables, because static initialization in the translation unit of `main` happens before `main` gets control, and static initialization in other translation units may happen before or after static initialization of `main` translation unit in unspecified order.

However, you can initialize a hidden non-const variable, and provide a `const` reference to it, like this:

```
struct A {
public: 
    // Expose T as a const reference to int
    static const int& T;
};

//in main.cpp

// Make a hidden variable for the actual value
static int actualT;
// Initialize A::T to reference the hidden variable
const int& A::T(actualT);

int main(int argc,char** argv) {
    // Set the hidden variable
    actualT = atoi(argv[1]);
    // Now the publicly visible variable A::T has the correct value
    cout << A::T << endl;
}

```

[Demo.](http://ideone.com/3gSNb8)
Not only you can't, you should not try doing this by messing with const\_cast. Static const members have a very high chance of ending up in read-only segment, and any attempt to modify them will cause program to crash.