Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do stream operators return references in C++?

Tags:

c++

stream

I know that indexing operator implementations usually return references so that the values can be set as well as retrieved, but why do streams?

like image 336
M.J. Rayburn Avatar asked Dec 12 '22 01:12

M.J. Rayburn


2 Answers

So you can chain them together.

cout << "hello" << "how are you";

Works because cout << "hello" returns a reference to the cout so that << "how are you" knows there to put itself.

Most operators, such as +=, also do this.

like image 146
Dylan Lawrence Avatar answered Feb 06 '23 09:02

Dylan Lawrence


Streams do not support copying or assignment, so anything that passes or returns a stream must use either a pointer or reference. You can't use overloaded operators on a pointer (without dereferencing it) because they'd try to apply the built-in operator to the pointer itself.

So, returning a reference is the only choice that supports operator chaining.

like image 24
Jerry Coffin Avatar answered Feb 06 '23 08:02

Jerry Coffin