Does return statement copy values

前端 未结 11 1823
夕颜
夕颜 2020-12-23 08:56

I am wondering about this because of scope issues. For example, consider the code

typedef struct {
    int x1;/*top*/
    int x2;/*bottom*/
    int id;
} sub         


        
11条回答
  •  不知归路
    2020-12-23 09:22

    yes , the return is a copy

    subline_t subline(int x1, int x2, int id) {
            subline_t t = { x1, x2, id };
            return t;
    }
    

    If you put a referencer, then its not a copy

    subline_t & subline(int x1, int x2, int id) {
            subline_t t = { x1, x2, id };
            return t; // will result in corruption because returning a reference
    }
    

提交回复
热议问题