Return std::string as const reference
Solution 1
Returning by reference or const reference has no speed difference - both are very fast as they just return a reference to the original object, no copying is involved.
An object returned by (non-const) reference can be modified through that reference. In your specific example, mString
is public, so it can be modified anyway (and directly). However, the usual approach with getters and setters (and the primary reason for their introduction) is encapsulation - you only allow access to your data members through the getter/setter, so that you can detect invalid values being set, respond to value changes and just generally keep the implementation details of your class hidden inside it. So getters normally return by const reference or by value.
However, if you return by const reference, it binds you to always keep an instance of std::string
in your class to back up the reference. That is, even if you later want to redesign your class so that it computes the string on the fly in the getter instead of storing it internally, you can't. You'd have to change your public interface at the same time, which can break code using the class. For example, as long as you return by const-reference, this is perfectly valid code:
const std::string *result = &aSample.Get();
This code will of course produce a dangling pointer no longer compile if Get()
is changed to return by value instead of const reference. (thanks to Steve Jessop for correcting me)
To sum up, the approach I would take is to make mString
private. Get()
can return by value or by const-reference, depending on how certain you are that you'll always have a string stored. The class would then look like this:
class sample
{
std::string mString;
public:
void Set(const std::string &s)
{
mString = s;
}
std::string Get() const
{
return mString;
}
};
Solution 2
The problem of deciding how to return a non-trivial object from some sort of a container is actually non-trivial:
- If the class from which you return your value imposes any sort of constraint on the object, you can't return a non-
const
reference because it would loose the possibility to enforce its invariants. Clearly, returning an object by non-const
reference is only viable if object the member function is called on is also non-const
. - Exposing a
const
reference to an object would avoid the problem with the invariants but still implies that an object of the corresponding type is actually kept internally as an implementation detail. - Returning an object by value may incur a significant cost for copying the object.
If you class is further a viable monitor you definitely want to return the object by value because otherwise the object can be mutated before the caller had any chance to copy it.
Basically, none of the choices is ideal. When in doubt, I return by value unless the object is known to be expensive to copy in which case I might return by const&
.
Solution 3
The most common thing to do here would be to return the value as a const-reference, then you can use a reference or copy the value as necessary:
const std::string& Get() const
{
return mString;
}
sample mySample;
const std::string &refString = mySample.Get(); // Const-reference to your mString
const std::string copyString = mySample.Get(); // Copy of your mString
If you really need to return a copy of the string, then you can avoid copying the string return value by utilising "The Most Important Const":
sample mySample;
const std::string &myString = mySample.Get();
// myString is now valid until it falls out of scope, even though it points to a "temporary" variable
Aneesh Narayanan
Updated on November 26, 2020Comments
-
Aneesh Narayanan over 3 years
I have a doubt on returning std::string as const reference.
class sample { public: std::string mString; void Set(const std::string& s) { mString = s; } std::string Get() { return mString; } };
In the Set function I am passing the std::string as const reference, const because its value is not changing inside the function.
And In Get function, actually I am confused here. Return std::string as value makes more sense. But I am not sure that, by passing the string as const reference makes any advantages. By returing string as reference will increase the exectuion speed, I think So, but I am not sure. But returning it as 'const makes any benefit for this?
-
Aneesh Narayanan over 11 yearsWhat about return it as 'const'??
-
chill over 11 yearsIf you want your string to be modified only by the
Set
method, defineGet
asconst std::string &Get () const { return mString; }
-
Steve Jessop over 11 years"This code will of course produce a dangling pointer if Get() is changed to return by value instead of const reference." -- no it won't, it will cease to compile ("can't take the address of a temporary"). Which is better than compiling but with UB. Actually it could compile due to an extension, but I don't think that's a common extension and the standard still calls for a diagnostic so the user should be at least warned.
-
Werner Erasmus over 8 yearsYes. I know this is an old question, but especially in the case of an interface, where one does not want to pose restrictions on implementation, by value returning is mostly better.
-
whoan over 5 years+1 for remarking if you return by const reference, it binds you to always keep an instance of std::string in your class to back up the reference
-
Melroy van den Berg over 2 yearsShould the const not be after std::string? Like so:
std::string const Get()
?? -
Angew is no longer proud of SO over 2 years@MelroyvandenBerg If you mean in the last example, then no. I don't want to return a
const std::string
; returningconst
objects is generally a bad idea as it prevents move operations and doesn't really help in anything - the caller can always do a copy and modify it. There are very few exceptional cases where it's warrented. Theconst
after a member function name designates the member function itself asconst
- that it doesn't modify its object and can thus be called throughconst
-qualified access paths (e.g. through aconst sample&
). -
Melroy van den Berg over 2 years@AngewisnolongerproudofSO Thank you! I will remove by unnecessary
const
in my code (std::string const Get()
-->std::string Get()
). -
Melroy van den Berg over 2 yearsPs. But
const
at the end is advised:std::string Get() const
? If the class object does not change with this getter..?