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: 
Method #1: Initialize a hidden non-const variable, and provide a `const` reference to it (as shown by dasblinkenlight):
-----------------------------------------------------------------------------------------------------------------------

```
class A {
public: 
  static const int &T;
};

static int dummy = 0;
const int &A::T  = dummy;

int main() {
  dummy = 10;
  std::cout << A::T << std::endl;
}

```

[**Live Demo**](http://coliru.stacked-crooked.com/a/13426bcab89e2690)

Method #2: Use a non `const` static member (as shown by R Sahu):
----------------------------------------------------------------

```
class A {
public: 
  static int T;
};

int A::T = 0;

int main() {
  A::T = 10;
}

```

[**Live Demo**](http://coliru.stacked-crooked.com/a/69ada8aeb2445af8)

Method #3: Declare a hidden non-const variable as a private static member of your class and provide a static member const reference to interface it. Define a friend function as inititalizer:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

```
class A {
  friend void foo(int);
    static int dummy;
public: 
    static const int &T;
};

const int &A::T = A::dummy;
int A::dummy = 0;

void foo(int val) { A::dummy = val; }

int main() {
    foo(10);
    std::cout << A::T << std::endl;
}

```

[**Live Demo**](http://coliru.stacked-crooked.com/a/3127283a05d6c0e9)

Method #4: Declare a hidden non-const variable as a private static member of your class and provide a static member const reference to interface it. Define a static member function as inititalizer:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

```
class A {
    static int dummy;
public: 
    static const int &T;
    static void foo(int val) { A::dummy = val; }
};

const int &A::T = A::dummy;
int A::dummy = 0;

int main() {
    A::foo(10);
    std::cout << A::T << std::endl;
}

```

[**Live Demo**](http://coliru.stacked-crooked.com/a/caf2077262c8ffde)

Bonus:
------

If you want to initialize only once you can change the helper function to:

```
static void foo(int val) { 
  static bool init = true;
  if(init) A::dummy = val;
  init = false;
}

```

[**Live Demo**](http://coliru.stacked-crooked.com/a/7116cb6a0bdae908)
Use a Singleton Pattern here.
have a data member which you'd like to initialize at run time in the singleton class. One a single instance is created and the data member is properly initialized, there would be no further risk of overwriting it and altering it.

Singleton would preserve the singularity of your data.

Hope this helps.