Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operator overloading for manipulators

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

  1. only existing operators can be overloaded. New operators cannot be created. But the symbol is not existing operator.

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

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

like image 736
Mohammed H Avatar asked Mar 28 '12 17:03

Mohammed H


People also ask

Which operators we can overload?

An overloaded operator is called an operator function. You declare an operator function with the keyword operator preceding the operator. Overloaded operators are distinct from overloaded functions, but like overloaded functions, they are distinguished by the number and types of operands used with the operator.

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.

Does overloading == also overload !=?

NO. There is no such requirement that you Must overload != If you need to overload == . However,it is a good practice that you Should overload operators related to each other.

What is operator overloading in op?

In computer programming, operator overloading, sometimes termed operator ad hoc polymorphism, is a specific case of polymorphism, where different operators have different implementations depending on their arguments. Operator overloading is generally defined by a programming language, a programmer, or both.


3 Answers

You are passing the function symbol to 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 :)

like image 59
Attila Avatar answered Oct 11 '22 14:10

Attila


std::ostream::operator<< is overloaded for function pointers of the type ostream &(*)(ostream &). The behaviour is simply to call the function on *this.

That's how things like std::hex work.

like image 26
Oliver Charlesworth Avatar answered Oct 11 '22 13:10

Oliver Charlesworth


There is an existing operator<< overload that is a member of basic_ostream

basic_ostream& operator<<(basic_ostream& (*pf)(basic_ostream&));

The signature matches your function (as well as std::endl, std::flush, etc).

like image 26
Bo Persson Avatar answered Oct 11 '22 14:10

Bo Persson