>> and << operator overloading

22,821

Solution 1

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.

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

Solution 3

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;

Solution 4

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:
22,821

Related videos on Youtube

Earlz
Author by

Earlz

Hello there! My name's Jordan Earls, but most people online know me as "earlz". I'm the lead developer and a co-founder of the Qtum project which brings the Ethereum Virtual Machine (ie, the thing that makes Solidity contracts function) to a UTXO based blockchain similar to Bitcoin. I've been programming since I was 13 and am completely self-taught. Low-level code like assembly and pointer arithmetic is the fun stuff for me. I also make music when I have time even though it's usually awful. Most of my personal projects are open source and BSD licensed. The majority of them are at bitbucket with the rest of them being listed on github Also, you can follow me on the twitters @earlzdotnet

Updated on January 03, 2020

Comments

  • Earlz
    Earlz over 4 years

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

    • radman
      radman over 13 years
      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 :)