operator overloading using ostream and chaining. Why return by reference?

后端 未结 5 1102
粉色の甜心
粉色の甜心 2021-02-10 19:24

There are many questions and answers for this, but I can\'t really find why we need to return by reference.

If we have (assume operator is already correctly ov

5条回答
  •  别跟我提以往
    2021-02-10 20:12

    The main reason is because returning by value makes a copy, and iostream objects are not copyable. They have state and identity, and it's not clear what copying them should mean: the object contains (logically, at least) its position in the stream, so if I create a copy, I have two objects which will write at the same position in the stream.

提交回复
热议问题