I think the following code can be used to create manipulators.
#include<iostream>
ostream & symbol(ostream & output)
{
return output << "\tRs";
}
it is working fine. The following statement
cout << "Total amount: " << 567 << symbol;
gives the output
Total amount: 567 Rs
But I didn’t understand why it is working.
I have the following information about operator overloading in C++.
-
only existing operators can be overloaded. New operators cannot be created.
But the symbol is not existing operator. -
In the statement (cout << “Total amount: ” << 567 << symbol;), it seems that << is the overloaded operator and symbol is a variable/object.
But I didn’t declare symbol as variable/object. -
why are they using the return statement (return output << “\tRs”;)?. I think (return “\tRs”;) or (output << “\tRs”;) should work.( I tried but not working 🙂 )
Actually I don’t know how the above code is working. Is there anybody to explain the working of the above operator overloading?
You are passing the function
symbolto the operator<<. The<<will call that function on the current ostream (with the ostream object as parameter), thus achieving the result you see. (The exact version of<<called is:ostream::operator<< (ostream& ( *pf )(ostream&));— see the reference for more info)The return type is
ostream, to allow chaining of multiple<<‘s. You would not need it technically in your particular case as<<has access to the stream, but this is to keep it consistent with the operators (I think). Of course<<requires this return parameter, so you have no choice 🙂