Returning a const reference to an object instead of a copy

前端 未结 12 561
天命终不由人
天命终不由人 2020-11-29 16:40

Whilst refactoring some code I came across some getter methods that returns a std::string. Something like this for example:

class foo
{
private:
    std::st         


        
相关标签:
12条回答
  • 2020-11-29 17:31

    Some implementations of std::string share memory with copy-on-write semantics, so return-by-value can be almost as efficient as return-by-reference and you don't have to worry about the lifetime issues (the runtime does it for you).

    If you're worried about performance, then benchmark it (<= can't stress that enough) !!! Try both approaches and measure the gain (or lack thereof). If one is better and you really care, then use it. If not, then prefer by-value for the protection it offers agains lifetime issues mentioned by other people.

    You know what they say about making assumptions...

    0 讨论(0)
  • 2020-11-29 17:32

    Depends what you need to do. Maybe you want to all the caller to change the returned value without changing the class. If you return the const reference that won't fly.

    Of course, the next argument is that the caller could then make their own copy. But if you know how the function will be used and know that happens anyway, then maybe doing this saves you a step later in code.

    0 讨论(0)
  • 2020-11-29 17:33

    Odds are pretty good that typical usage of that function won't break if you change to a const reference.

    If all of the code calling that function is under your control, just make the change and see if the compiler complains.

    0 讨论(0)
  • 2020-11-29 17:37

    It is conceivable that you could break something if the caller really wanted a copy, because they were about to alter the original and wanted to preserve a copy of it. However it is far more likely that it should, indeed, just be returning a const reference.

    The easiest thing to do is try it and then test it to see if it still works, provided that you have some sort of test you can run. If not, I'd focus on writing the test first, before continuing with refactoring.

    0 讨论(0)
  • 2020-11-29 17:43

    One problem for the const reference return would be if the user coded something like:

    const std::string & str = myObject.getSomeString() ;
    

    With a std::string return, the temporary object would remain alive and attached to str until str goes out of scope.

    But what happens with a const std::string &? My guess is that we would have a const reference to an object that could die when its parent object deallocates it:

    MyObject * myObject = new MyObject("My String") ;
    const std::string & str = myObject->getSomeString() ;
    delete myObject ;
    // Use str... which references a destroyed object.
    

    So my preference goes to the const reference return (because, anyway, I'm just more confortable with sending a reference than hoping the compiler will optimize the extra temporary), as long as the following contract is respected: "if you want it beyond my object's existence, they copy it before my object's destruction"

    0 讨论(0)
  • 2020-11-29 17:46

    Returning a reference to a member exposes the implementation of the class. That's could prevent to change the class. May be usefull for private or protected methods incase the optimization is needed. What should a C++ getter return

    0 讨论(0)
提交回复
热议问题