When should you return a reference to a object from a class method

后端 未结 7 1755
旧时难觅i
旧时难觅i 2021-02-14 01:14

What is the best practice for returning references from class methods. Is it the case that basic types you want to return without a reference whereas class objects you want to r

相关标签:
7条回答
  • 2021-02-14 01:43

    I would recommend staying away from returning references for the same reason as Iraimbilanja points out, but in my opinion you can get very good results by using shared pointers (e.g., boost, tr1) on the member data and use those in return. That way you do not have to copy the object but can still manage life time issues.

    class Foo
    {
    private:
        shared_ptr<Bar> _bar;
    public:
        shared_ptr<Bar> getBar() {return _bar;}
    };
    

    Usually the cost of copying Bar is greater than the cost of constructing new shared_ptrs, should this not be the case it can still be worth using for life time management.

    0 讨论(0)
  • 2021-02-14 01:44

    Overloading assignment operators (like =, +=, -= etc.) is a good example where returning by reference makes a lot of sense. This kind of methods would obviously return large objects, and you don't want to get back pointers, so returning a reference is the best way to go. Works like a pointer and looks like returning by value.

    0 讨论(0)
  • 2021-02-14 01:50

    A reference is a pointer in disguise (so it's 4 bytes on 32 bit machines, and 8 bytes on 64 bit machines). So the rule of thumb is: if copying the object is more expensive than returning a pointer, use a pointer (or reference, since that's the same thing).

    Which types are more expensive to copy depends on the architecture, compiler, the type itself etc. In some cases copying an object that's 16 bytes can be faster than returning a pointer to it (for example, if an object maps to SSE register, or similar situation).

    Now, of course, returning a reference to a local variable does not make sense. Because the local variable will be gone after the function exits. So usually you'd return references/pointers to member variables, or global/static variables, or dynamically allocated objects.

    There are situations where you don't want to return pointer/reference to an object, even if copying the object is expensive. Mostly when you don't want to tie the calling code into the lifetime of the original object.

    0 讨论(0)
  • 2021-02-14 01:50

    if NULL is a possible return value, the method must return a pointer because you can't return a reference to NULL.

    0 讨论(0)
  • 2021-02-14 01:52

    Scott Meyers' book, Effective C++, has several items related to this topic. I would definitely check out the item titled, "Don't try to return a reference when you must return an object." This is item #23 in the 1st or 2nd editions, or #21 in the 3rd edition.

    0 讨论(0)
  • 2021-02-14 01:52

    Return basic types by value, except if you want to let the caller access the actual member.

    Return class objects (even std::string) by reference.

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