Documente Academic
Documente Profesional
Documente Cultură
4.1 Objectives
To understand the fundamentals of operator overloading
To understand the differences between overloading as member function and non-member friend
function
An operator function definition can be done similarly, with the exception that function name must
take the form of operatorX, where X is the symbol for an operator, e.g. +,-, /, etc. For instance, to
declare multiplication operator the function prototype would look like
Therefore, function overloading principle also applies to operators. Operator overloading can be
implemented by having different type of argument(s).
a.add(b);
a+b;
Could you compile it? What did you observe from the execution window? Why did the program
compile just fine? It is because of the signatures for both functions are different and therefore
distinguishable. Thanks to function overloading.
Most of us have been using the operators such as +, -, *, & etc. and pay little attention about what
actually is happening. We can easily add an integer with another integer( int + int ), or add a double
with a float ( double + float ) using the very same operator symbol + without questioning anything.
How do they work?
Note: Do you realize that all overloadable operators are overloaded already? Can you give
examples?
+ - * / % ^ & | ~
!
= < > += -= *= /= %= ^=
|=
<< >> >>= <<= == != <= >= &&
||
++ -- ->* , -> [] () new delete
new[] delete[]
. .* :: :? sizeof
Warning: not to abuse operator overloading such that blurs the way how a particular operator
operates normally.
The (=) assignment operator and the (&) address operator are not needed to be overloaded since both
can work automatically with whatever data types. (=) assignment operator creates a bit-by-bit copy
of an object while the (&) address operator returns a memory address of the object. The exception
comes when we deal with classes containing pointers as members. In this case, the assignment
operator needs to be overloaded explicitly.
a+b;
These two arguments shall appear as the arguments of the operator+ function. However,
since here we choose to implement it as a member function. Recall about how the this pointer
works! When we have such a statement in our program, it will be interpreted by the compiler as the
left operand, a calls its member function operator+ with b as the argument.
a.operator+(b);
Next, the return value type will naturally be a Fraction object as well (You have choice
whether to return a Fraction object or a reference to Fraction object, though). So, now we have
sufficient knowledge on how to declare the prototype function. It will look like
The usage of the overloaded function, whether implemented as member function or non-
member (friend) function does not have any difference. Exactly, the same as given in Fig. 4.4.
Note, however, that the compiler interprets the same statement slightly differently. When it sees
statement
a+b;
it is interpreted as
operator+( a, b );
Then, your curious mind may question: what is the motivation for choosing member
function over non-member (friend) function or vice versa if that’s the only difference. We would
agree with you. Before we proceed, please modify example in Fig. 4.4, the statement
c = a + b;
with
c = b + a;
What is your observation?
The same result, c=a+b = b+a. It is because the two operands a and b are of the same
type. Now let’s consider adding class Fraction object a and an integer variable b. We have two
objects of different types. Here we assume that the result, c is of type Fraction.
c = a + b;
c = b + a;
Will we be able to implement operator+ as either member function or non-member (friend)
function. Let’s implement it as member function first.
Fig. 4.6 Overloading operator+ with operands of different data types as member function
What do you observe from Fig. 4.7(a) and (b)? The overloaded operator+ does work for
a+b but does NOT work for b+a. Do you know why?
Next, we will overload operator+ as non-member friend function.
Fig. 4.8 Overloading operator+ with operands of different data types as non-member function