Whats the significance of return by reference?

后端 未结 11 1222
既然无缘
既然无缘 2021-02-13 00:15

In C++,

function() = 10;

works if the function returns a variable by reference.

What are the use cases of it?

11条回答
  •  无人共我
    2021-02-13 01:21

    SO screwed up my answer

    You don't even need to return a reference:

    struct C { };
    
    C f() {
      return C();
    }
    
    int main() {
      C a;
      f() = a;  // compiles fine
    }
    

    Because this behavior is quite surprising, you should normally return a const value or a const reference unless the user has a sensible intent to modify the result.

提交回复
热议问题