6

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..

2
  • 3
    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, 2010 at 5:00
  • @radman I've emailed my teacher about the question and waiting for a reply now
    – Earlz
    Nov 1, 2010 at 5:05

6 Answers 6

19

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.

0
2

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;
2

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

To say 'must' is incorrect, probably 'usually' is the correct word, and why? Because as most of the answers have already pointed out, it gives the convenience of object chaining, while working with iostreams.

1

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.

0

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

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.