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 constructor I invoke a function that uses these static strings. Surprisingly when in constructor, the strings remain uninitialized (empty strings) which is creating a problem.
Can somebody point out what might be going wrong here? I work with such usage of static const strings all the time but never ran into such situations.
Update: m_data remains empty in utility(). I have a Test class object as a private member of another class.
Here is a kind of code I am 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
}
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.
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)
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.
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!
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.
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.