There will be a problem if your code looks like:
std::string& MyFunc() { std::string mystring("test"); return mystring; }
So, as you wrote it, everything is in order. Only one piece of advice: if you can build such a line, I mean - you can do it on one line, sometimes itβs better to do it like this:
std::string MyFunc() { return "test"; }
Or, if it is more "complicated", for example:
std::string MyFunct( const std::string& s1, const std::string& s2, const char* szOtherString ) { return std::string( "test1" ) + s1 + std::string( szOtherString ) + s2; }
This will give your compiler a hint to do a lot of optimization so that it can make one smaller copy of your string (RVO).
Kiril Kirov Oct 20 '10 at 11:09 2010-10-20 11:09
source share