I was working on a little project and came to a situation where the following happened:
std::string myString;
#GetValue() returns a char*
myString = myObject
It is runtime error.
You should do this:
myString = ValueOrEmpty(myObject.GetValue());
where ValueOrEmpty is defined as:
std::string ValueOrEmpty(const char* s)
{
return s == nullptr ? std::string() : s;
}
Or you could return const char* (it makes better sense):
const char* ValueOrEmpty(const char* s)
{
return s == nullptr ? "" : s;
}
If you return const char*, then at the call-site, it will convert into std::string.