I believe the second implementation (const reference) is correct as:
- the returned object is immutable and therefore upholds the rules of encapsulation.
- it's slightly more efficient as there is no copying of
str.
However the first approach will work almost as well.