Consider this code:
void doSomethingWithString(string& mString) { // mString gets modified in here } string getCopy1(const string& mString) { string result{mString}; doSomethingWithString(result); return result; } string getCopy2(string mString) { doSomethingWithString(mString); return mString; } Between getCopy1 and getCopy2, what one:
- Clearly shows that the passed string isn't going to get modified
- Clearly shows that the user will get a new string returned
- Is faster / can be better optimized by the compiler (C++11 enabled, consider move semantics)
?
getCopy2it is.