Why does overloading ostream’s operator<< need "&"?

I’ve been learning C++.

From this page, I understood that overloading “<<” operator of ostream can be done in this way.

ostream& operator<<(ostream& out, Objects& obj) {
    //
    return out;
} 
//Implementation

and

friend ostream& operator<<(ostream& out, Object& obj);
//In the corresponding header file

My question is… why does this function need “&” at the end of ostream and Object?

At least I know that “&” is used to…

  1. Take the address of a value
  2. Declare a reference to a type

However, I don’t think neither of them applies to the overloading described above. I’ve spent a lot of time in googling and reading a text book, but I can’t find the answer.

I’d appreciate if you’d give advice on it.


Source: c++

Leave a Reply