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

后端 未结 7 943
醉梦人生
醉梦人生 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条回答
  •  萌比男神i
    2020-12-02 15:36

    It's returned by whatever you declare the return type to be. vector f(); and vector& f(); return by value and reference respectively. However, it would be a grave error to return a reference to a local variable in the function as it will have been blown away when the function scope exits.

    For good tips on how to efficiently return large vectors from a function, see this question (in fact this one is arguably a duplicate of that).

提交回复
热议问题