Static constant string will not be initialized

I have some static constant strings as private members of my C ++ class. I know the declaration in .h and the definition (and initialization) in .cpp practice. In the class constructor, I call a function that uses these static lines. Surprisingly, in the constructor, strings remain uninitialized (empty strings), which creates a problem.

Can someone point out what might be wrong here? I have been working with this use of static constant strings all the time, but have never come across such situations.

Update: m_data remains empty in utility (). I have an object of class Test as a private member of another class.

Here is the code I'm using:

// Test.h class Test { public: Test(); private: void utility(); static const std::string m_data; }; // Test.cpp const std::string Test::m_data = "Data"; Test::Test() { utility(); } void Test::utility() { //use m_data here } 
+4
source share
6 answers

Is your object of type TEST global?

If so, you have encountered a problem with the initialization order.

t

 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. 
+5
source

Do you define it as such?

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

See: Static data members (C ++ only)

+4
source

Based on the current code, I would suggest that you are trying to create a global test instance, most likely in another .cpp file. It looks like you are faced with a terrible static fiction of the initialization order . Simply put: the global / static-member variables in the .cpp file will be initialized in the order in which they are defined. There are no guarantees regarding global variables in different .cpp files; you cannot rely on var in one file that is initialized before or after a global variable in another.

+3
source

That's 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 : constructor blah - line initialized: Hello, Blah!

+1
source

I will test the program and it worked perfectly. What IDE environment do you work in? Is it right on the windows?

You can use, if I'm wrong, make a determination if the class itself, where you declare the member, causes it to be static.

0
source

I would use a different approach:

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

I personally prefer this "style", and it eliminates any problems with initializing the data in the constructor, which can be a minor cost to build a string for each instance.

0
source

All Articles