Take the 2-minute tour ×

I just did a quiz for my programming class and got this question wrong:

The return type of the function to overload the operator << must be a reference to an ostream object.

This does not seem right at all to me. Surely C++ is a bit more open ended than this. But I thought I'd ask here anyway. How is this right (or wrong)? My C++ knowledge begins to really fade when it comes to operator overloading..

share|improve this question
Sounds like a badly worded question to me, the teacher obviously meant it within the context of stream operators but failed to state that explicitly. Operator overloading enforces no such constraints and as such your answer was correct, I encourage you to notify them of their stupidity :) –  radman Nov 1 '10 at 5:00
@radman I've emailed my teacher about the question and waiting for a reply now –  Earlz Nov 1 '10 at 5:05

6 Answers 6

up vote 10 down vote accepted

It is not required by C++ that the return type be a reference to an ostream object. However, if you are trying to do something like:

cout << instance_of_custom_type << 3 << "hi" << endl;

Then you will need:

ostream &operator << (ostream &os, custom_type &t);

However, if you were doing something like writing a large integer type, and wanted to support bit shifting, it might be something like:

BigInt operator << (const BigInt &i, unsigned int shift);

To expand this a bit further, the original use of the << operator is for bit shifting. 1 << 8 is 256, for example. C++ added a (slightly confusing) second use for this, and overloaded it on ostream to mean "output" to the stream. You can do whatever you like within an overloaded operator - it works just like a function, however, operators have a human expectation attached with them: programmers expect, in C++, that << is bit shifting or stream output.

share|improve this answer
This I think is what the quiz was implying, but worded it ambiguously.. –  Earlz Nov 1 '10 at 4:41

Having the return type as a refernce to the same stream object passed as reference argument to the overloaded insertion operator enables us to write code such as

mystream &operator << (mystream &os, myclass &myobject){
   // do whatever
   return os;

mystream << myobject << fundamental_type_object;
share|improve this answer

To say Must is incorrect, probably Usually is the correct word, and Why? becuase as most of the answers have already pointed out, It gives the convenience of Object Chaining, while working with Iostreams.

share|improve this answer

The purpose of having it return the ostream reference is so that you can chain them together. Otherwise you'd have to write cout << 1; cout << " is a number"; cout << endl

share|improve this answer

From the more general point of view, operator<< should always return it's left hand side operand in order to chain calls, just like operator=.

When dealing with the <iostreams> library, this happens to be a reference to std::ostream.

share|improve this answer

It isn't right. It's only correct in the context of iostreams, which in my probably irrelevant and uninteresting opinion should never have been let out of the cage in that form. If you don't include iostreams in your code you can do what you like. But I wouldn't be overloading these operators to do anything except shift classes, whatever that means, by integer values, or maybe by classes that can be reduced to integer values somehow.

share|improve this answer

Your Answer


