Function in C++ returns by value or by reference?

后端 未结 7 837
醉梦人生
醉梦人生 2020-12-02 14:36

When a function (callee) returns a quantity to the caller function, is it returned by value or by reference?

The thing is I have written a function which builds a ve

相关标签:
7条回答
  • 2020-12-02 15:38

    By default, everything in C/C++ is passed by value, including return type, as in the example below:

    T foo() ;
    

    In C++, where the types are usually considered value-types (i.e. they behave like int or double types), the extra copy can be costly if the object's construction/destruction is not trivial.

    With C++03

    If you want to return by reference, or by pointer, you need to change the return type to either:

    T & foo() ;  // return a reference
    T * foo() ;  // return a pointer
    

    but in both cases, you need to make sure the object returned still exists after the return. For example, if the object returned was allocated on stack in the body of the function, the object will be destroyed, and thus, its reference/pointer will be invalid.

    If you can't guarantee the object still exists after the return, your only solution is to either:

    1. accept the cost of an extra copy, and hope for a Return Value Optimization
    2. pass instead a variable by reference as a parameter to the function, as in the following:

    void foo(T & t) ;

    This way, inside the function, you set the t value as necessary, and after the function returns, you have your result.

    With C++11

    Now, if you have the chance to work with C++0x/C++11, that is, with a compiler that supports r-values references/move semantics, if your object has the right constructor/operator (if your object comes from the standard library, then it's ok), then the extra temporary copy will be optimized away, and you can keep the notation:

    T foo() ;
    

    Knowing that the compiler will not generate an unnecessary temporary value.

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