Operator Overloaded Functions as Non-Member Functions

Written by Raza on. Posted in C++

C allows almost all the operators to be overloaded both as member and non-member functions. Up till now we have overloaded all the operators as member functions. Today we shall overload addition operator for our class as a non-member function. We shall also look at the advantages and disadvantages of declaring an overloaded function for the operator as a non-member function. 

 

Why overload operators as non-member function:

When we overloaded the addition operator, we saw that we call the function with this sort of syntax. Suppose it for our Fraction Class.

</p>
<p>Fraction f1 = f2 f3 ;</p>
<p>

 

Here, the overloaded function for the plus operator is called upon f2 and f3 is being passed to it as its argument. If we reverse the order, then function would be called on f3 with f2 passed as argument. In this case, the addition is commutative and you can safely change the order. However in some cases, you cannot reverse the order. That is when you add a non-object value to the object. Here is the case.

</p>
<p>Fraction f1 = f2 <a href="http://www.svenskkasinon.com/">casino pa natet</a>  2 ;</p>
<p>

 

The compiler says, call the overloaded function for addition operator on f2 and pass integer 2 as argument to it. Inside the function, integer 2 is added to the object. You do it like this.

</p>
<p>Fraction = 2 f2 ;</p>
<p>

 

This is not allowed. The compiler gives an error. The reason is simple as I’ve mentioned in the rules for operator overloading. For member functions of overloaded operators, the object which calls that function should be on the right hand side of the operator and its argument on the right hand side. In this case, what can the compiler say? It says call the overloaded function for plus operator on integer 2 and send f2 as its argument. Clearly the function cannot be called on an integer even if you reverse the order of parameters in the function.

If the overloaded function for plus operator was declared as a non-member function, this syntax could be legal. We would just have to change position of parameters in the function. So it is easy and gives no harm to overload the overloaded function which does not belong to the class.

 

Source code for Overloaded Function for Plus operator:

The overloaded function for addition operator would take two parameters. One the object and second the integer value to be add. Both cold be objects as well depending upon what functionality you want. Below is the code for it.

</p>
<p>Fraction operator (int number, const Fraction &amp;obj)<br />
{<br />
.  Fraction temp ;<br />
.  temp.num = obj.num (obj.den * number) ;<br />
.  temp.den = obj.den ;<br />
.  return temp ;<br />
}</p>
<p>&nbsp;</p>
<p>int main ()<br />
{<br />
.  Fraction f1 (3, 2) ;<br />
.  cout &lt;&lt; 2 f1 ;<br />
}</p>
<p>

Keep it in mind that the integer is the first parameter in the function to allow the syntax in the main. If we reverse the order with integer second in the list, this syntax would not be possible.

Tags: , , , ,