Return std :: string as a reference to a constant

I have a doubt in returning std :: string as a reference to a constant.

class sample { public: std::string mString; void Set(const std::string& s) { mString = s; } std::string Get() { return mString; } }; 

In the Set function, I pass std :: string as a constant const, const, because its value does not change inside the function.

And in the Get function, I'm actually confused here. We return std :: string, since the value makes more sense. But I'm not sure if passing a string as a const reference, you get any advantages. By holding the string as a link, you will increase the speed of exectuion, I think so, but I'm not sure. But returning it as "const", is there any benefit from this?

+7
source share
4 answers

Returning by reference or constant link has no difference in speed - both are very fast, as they simply return a link to the original object, and copying is not performed.

The object returned by the link (not const) can be modified using this link. In your specific example, mString is publicly available, so you can change it anyway (and directly). However, the usual approach with getters and setters (and the main reason for their implementation) is encapsulation - you only allow access to your data members through the getter / setter so that you can define invalid values ​​that are set, respond to changes in values, and simply save implementation details of your class hidden inside it. Thus, recipients usually return by constant reference or by value.

However, if you return with a const link, it will link you to always keep an instance of std::string in your class to backup the link. That is, even if you later want to redesign your class so that it calculates the string "on the fly" in the getter, rather than storing it internally, you cannot. You must simultaneously change your public interface, which can break code with a class. For example, as long as you return using const-reference, this is perfectly valid code:

 const std::string *result = &aSample.Get(); 

This code, of course, creates a dangling pointer that no longer compiles if Get() modified to return by value instead of referencing a constant. (thanks to Steve Jessop for correcting me)

To summarize, the approach I would like to take is to make mString private. Get() can be returned by value or by const reference, depending on how confident you are that you will always store the string. The class will look like this:

 class sample { std::string mString; public: void Set(const std::string &s) { mString = s; } std::string Get() const { return mString; } }; 
+10
source

The problem of deciding whether to return a nontrivial object from any container is actually nontrivial:

  • If the class from which you return your value imposes any restriction on the object, you cannot return the const link, because it will lose the ability to use its invariants. Obviously, returning an object using the < const reference is viable if the object on which the member function is called is also not const .
  • Displaying a const reference to an object avoids the problem with invariants, but still implies that an object of the corresponding type is actually stored inside as an implementation detail.
  • Returning an object by value can entail significant costs for copying an object.

If your class is an even more viable monitor , you definitely want to return the object by value, because otherwise the object could be mutated before the caller had a chance to copy it.

In principle, none of the options is perfect. If in doubt, I return by value if the object is known to be expensively copied, in which case I could return to const& .

+13
source

Return it as a reference. If a copy is needed, it can, of course, be made from this certificate.

0
source

The most common task here would be to return the value as a reference to const, then you can use the link 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, you can avoid copying the return value of the string using " " Most Important Constant " ":

 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 
0
source

All Articles