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.
No, you cannot do that.
You can use a non-
const
member.Another option is to make
T
a private member, makemain
a friend so only it can modify the value, and then expose the member through a function.I am sorry to disagree with the comments and answers saying that it is not possible for a
static const
symbol to be initialized at program startup rather than at compile time.Actually this IS possible, and I used it many times, BUT I initialize it from a configuration file. Something like:
As you see, these static consts are not necessarily known at compile time. They can be set from the environment, such as a config file.
On the other hand, setting them from argv[], seems very difficult, if ever feasible, because when main() starts, static symbols are already initialized.
You cannot rely on data produced after your
main
has started for initialization ofstatic
variables, because static initialization in the translation unit ofmain
happens beforemain
gets control, and static initialization in other translation units may happen before or after static initialization ofmain
translation unit in unspecified order.However, you can initialize a hidden non-const variable, and provide a
const
reference to it, like this:Demo.
Typically you will have more than one configuration value. So put them in a struct, and the normal global access to it is const.
You can get fancier and have a global function to return config, so normal code can't even change the pointer, but it is harder to do that by accident.
A header file exposes
get_config ()
for all to use, but the way to set it is only known to the code that's meant to do so.Method #1: Initialize a hidden non-const variable, and provide a
const
reference to it (as shown by dasblinkenlight):Live Demo
Method #2: Use a non
const
static member (as shown by R Sahu):Live Demo
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:
Live Demo
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:
Live Demo
Bonus:
If you want to initialize only once you can change the helper function to:
Live Demo
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.