Aneesh Narayanan
Aneesh Narayanan

Reputation: 3434

Return std::string as const reference

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?

Upvotes: 17

Views: 47293

Answers (4)

Dietmar Kühl
Dietmar Kühl

Reputation: 153792

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 lose 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&.

Upvotes: 16

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;
  }
};

Upvotes: 14

Mark Ingram
Mark Ingram

Reputation: 73585

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

Upvotes: 7

chill
chill

Reputation: 16878

Return it as a reference. If a copy is needed, it certainly can be made from that reference.

Upvotes: 0

Related Questions