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..
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++.
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.
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.
7. Which function overloads the >> operator? Explanation: __rshift__() overloads the >> operator.
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.
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;
The return type of the function to overload the operator
<<
must be a reference to anostream
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
.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With