I have a function Analyze_, that returns a std::string with some info in it, I
want it in LPCSTR form though.
When I use c_str() like this, it works and l stores my information:
std::string s = myClassInstance.Analyze_();
LPCSTR l = s.c_str();
LPCSTR l = myClassInstance.Analyze_().c_str();
ss << "Bla blub...\n";
c_str() returns a
const char* to the internal buffer of the
std::string. Chaining method calls like you're doing here
LPCSTR l = myClassInstance.Analyze_().c_str(); creates a temporary
std::string (the return of
Analyze()) and then returns the pointer to the internal buffer of that string. Then, the string is destroyed and you're left with a dangling pointer, Undefined Behaviour when trying to derefence. Just save the
std::string if you want to use a pointer to it's buffer.