T
Thomas Lenz
Please consider the following code snippet:
string myfunction()
{ ostringstream oss;
oss << "junk";
// do something more with oss; I can't make it const...
return oss.str();
}
Is the returned string object still valid after myfunction() has returned? I
wonder because oss should be destroyed at the bottom '}', shouldn't it? So
what about its string? (The above code seems to work on my machine, but is
it guaranteed to work?)
Or is it safer to explicitly construct the string to return as a temporary
object, like
return string(oss.str());
?
Thanks in advance for any comments,
Thomas
string myfunction()
{ ostringstream oss;
oss << "junk";
// do something more with oss; I can't make it const...
return oss.str();
}
Is the returned string object still valid after myfunction() has returned? I
wonder because oss should be destroyed at the bottom '}', shouldn't it? So
what about its string? (The above code seems to work on my machine, but is
it guaranteed to work?)
Or is it safer to explicitly construct the string to return as a temporary
object, like
return string(oss.str());
?
Thanks in advance for any comments,
Thomas