Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

>> and << operator overloading

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

like image 461
Earlz Avatar asked Nov 01 '10 04:11

Earlz


People also ask

What is the advantage of overloading the << and >> operators?

By overloading the operators, we can give additional meaning to the operators like +-*/=.,= etc., which by default are supposed to work only on standard data types like int, float, char, void etc. It is an essential concept in C++.

Can we overload << operator?

We can overload the '>>' and '<<' operators to take input in a linked list and print the element in the linked list in C++. It has the ability to provide the operators with a special meaning for a data type, this ability is known as Operator Overloading.

Can we overload << operator in C++?

You can redefine or overload the function of most built-in operators in C++. These operators can be overloaded globally or on a class-by-class basis. Overloaded operators are implemented as functions and can be member functions or global functions. An overloaded operator is called an operator function.

Which function overload the >> operators?

7. Which function overloads the >> operator? Explanation: __rshift__() overloads the >> operator.


4 Answers

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.

like image 175
Thanatos Avatar answered Oct 26 '22 12:10

Thanatos


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;
like image 34
Chubsdad Avatar answered Oct 26 '22 10:10

Chubsdad


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.

like image 27
Alok Save Avatar answered Oct 26 '22 11:10

Alok Save


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.

like image 40
André Caron Avatar answered Oct 26 '22 12:10

André Caron