Static const string won't get initialized

后端 未结 6 1645
一生所求
一生所求 2021-01-13 21:00

I have some static const strings as private members of my C++ class. I am aware of the declaration in .h and definition (and initialization) in .cpp practice. In the class c

相关标签:
6条回答
  • 2021-01-13 21:25

    Are you defining it as such?

    class X
    {
    public:
          static string i;
    };
    string X::i = "blah"; // definition outside class declaration
    

    See: Static data members (C++ only)

    0 讨论(0)
  • 2021-01-13 21:26

    Is this what you need?

    class blah{
        static const string sz;
    public:
        blah(){
            cout<<"blah constructor - string initialized to: "<<sz.c_str()<<endl;
        }
    };
    
    const string blah::sz("Hello, Blah!");
    
    int _tmain(int argc, _TCHAR* argv[]){
        blah b;
        return 0;
    }
    

    Program Output: blah constructor - string initialized to: Hello, Blah!

    0 讨论(0)
  • 2021-01-13 21:27

    Is your object of type TEST a global?

    If so you are then running into the problem with initialization order.

    ie.

    int main()
    {
        std::cout << "Main Entered" << std::endl;
        Test  t; // This should work
    }
    Test  plop; // This may not work depending
    

    The solution is to use a static method to get the string:

    class Test
    {
        static std::string const& getData()
        {
            static std::string const data("PLOP");
            return data;
        }
        // STUFF
        // Remove this line
        // static const std::string m_data;
        Test::Test()
        {
            std::cout << "Test::Test()" << std::endl;
            Utility();
        }
    };
    // If "Test::Test()" is printed before "Main Entered"
    // You have a potential problem with your code.
    
    0 讨论(0)
  • 2021-01-13 21:40

    Based on current code I'd guess you try to create global Test instance, most probably in a different .cpp file. Seems like you are hitting the dreadful static initialization order fiasco. Simply put: the global/static-member variables in a .cpp file will be initialized in the order they are defined. No guarantees are made about global variables in different .cpp files, you cannot rely on var in one file being initialized before or after global variable in another.

    0 讨论(0)
  • 2021-01-13 21:41

    I mase a check of the program anbd it worked fine. In what IDE are you working with ? This on windows right ?

    You can use if I am mistaken make the definition if the class itself where you declare the member, the reason cause it is a const static.

    0 讨论(0)
  • 2021-01-13 21:47

    I'd use a different approach:

    class Test
    {
    public:
        Test() : m_data( "Data" ) {}
    private:
         const std::string m_data;
    };
    

    I personally prefer this 'style' and it would remove any issues with having initialised data in the constructor at what is likely to be the minor cost of constructing the string for each instance.

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