I am moving my project from Visual Studio 06 to 2010. While doing so, I have observed this behavior in my code. I have a Get string function that look like this:
string GetTheStr()
{
return strSomeStdString;
}
Then there is another function that call above get function like this:
const char* ptrStr = (char *)GetTheStr().c_str();
the value of string pointed by ptrStr is ""
above code was working fine in visual studio 06 but not on visual studio 2010.
Then I tried few experiments:
std::string str = GetTheStr(); // -> value inside str displayed correctly
const char* PtrCStr = str.c_str(); // -> value pointed by PtrCStr displayed correctly
const char* PtrData = str.data(); // -> value pointed by PtrData displayed correctly
const char* ptr = (char *)GetTheStr().c_str(); // -> value pointed by ptr NOT displayed correctly
I am wondering why last line didn't work.
Can anyone please tell me why above behavior happen in visual studio 2010 but not on visual studio 06?
Thanks in advance :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…