What's the ampersand for when used after class name like ostream& operator <<(...)?

Omar picture Omar · Oct 15, 2009 · Viewed 36.9k times · Source

I know about all about pointers and the ampersand means "address of" but what's it mean in this situation?

Also, when overloading operators, why is it common declare the parameters with const?

Answer

Kyle Walsh picture Kyle Walsh · Oct 15, 2009

In that case you are returning a reference to an ostream object. Strictly thinking of ampersand as "address of" will not always work for you. Here's some info from C++ FAQ Lite on references.

As far as const goes, const correctness is very important in C++ type safety and something you'll want to do as much as you can. Another page from the FAQ helps in that regard. const helps you from side effect-related changes mucking up your data in situations where you might not expect it.