Andre Pang wrote:
Erik de Castro Lopo wrote:
Here's a question for all the C++ crack monkeys. Is it possible
to return an std::string from a function?
Something like:
std::string &
return_string ()
{
std::string s ;
// Do something with s.
return s ;
}
You need to return an actual std::string, not a reference to one (the
latter is invalid C++). The normal idiom is to mark the returned
string as a const reference in the calling function:
It's not invalid to return a reference to a string. You just have to be
mindful of the scope of the data you are trying to return.
string& addStuffToString(string& s)
{
s = s + " stuff";
return s;
}
_______________________________________________
coders mailing list
[email protected]
http://lists.slug.org.au/listinfo/coders