Can I initialize a static const member at run-time in C++?

前端 未结 11 1067
小蘑菇
小蘑菇 2020-12-09 16:22

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 arg

相关标签:
11条回答
  • 2020-12-09 16:53

    There is a trick, but you should probably avoid it! Here's a bare bones example to illustrate the principle:

    int const& foo(int i) {
        static const int j = (i == 0 ? throw 0 : i); 
        return j; 
    }
    
    int main() {
        try { 
            int x = foo(0); // oops, we throw        
        } catch(...) {}
    
        int x = foo(1); // initialized..
        int y = foo(0); // still works..     
    }
    

    Careful!

    0 讨论(0)
  • 2020-12-09 16:54

    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.

    0 讨论(0)
  • 2020-12-09 16:54

    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.

    0 讨论(0)
  • 2020-12-09 16:56

    Typically you will have more than one configuration value. So put them in a struct, and the normal global access to it is const.

    const config* Config;
    ...
    main (int argc, char* argv [])
    {
    Config= new config (argc, argv);
    ...
    }
    

    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.

    0 讨论(0)
  • 2020-12-09 16:57

    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.

    0 讨论(0)
提交回复
热议问题