Overloaded operators are based on an operator notation but are implemented as functions. A thorough understanding of operators, functions, and how class member functions are called is essential for understanding overloaded operators. Please review the following as needed:
public
and private
keywords)Although overloaded operators and friend functions are independent constructs, meaning that it is possible to use one without the other, programmers often use them together. Java does not support either, so the concepts may be new for many students. Fortunately, both overloaded operators and friend functions are simple extensions to parts of C++ that we have already studied and used, making them an easy addition to our programming toolkit.
Overloaded operators are just functions dressed up with a bit of new syntax called with a specialized but familiar operator notation. For example, C++ allows programmers to add two integers using the "+" operator: 5 + 10
, which looks like the way we would write the operation using "pencil and paper." Similarly, we can write code that uses operators to call functions by overloading the operators for programmer-defined classes. For example, if f1 and f2 are instances of a class named fraction, their sum can be written with an overloaded "+" operator as: f1 + f2
, which looks like what we might write the operation on a whiteboard or a piece of paper.
Once you get past the new syntax and a bit of terminology, you'll find that you can apply all of your knowledge about functions, both members and non-members, to overloading operators. We have been using overloaded operators all semester but haven't called them that. The inserter, <<
, and the extractor, >>
, operators were originally used in C (and can still be used in C++) as the left and right bit-shift operators. C++ overloads (i.e., reuses) these two operators in the context of streams as the output and input operators, respectively.
Overloaded operators are implemented as functions with a special function name: operator☺
where ☺ is replaced by the operator to be overloaded. For example, the add function from the Time class example, Time add(Time t);
may be transformed into the overloaded operator Time operator+(Time t);
. There are a few basic characteristics that all C++ operators share and that overloaded operators cannot change:
When creating new overloaded operators, programmers cannot
Additionally, overloaded operators should be intuitive, that is, used in a natural way. (For example, if you create a class called Number, you shouldn't define "+" to perform subtraction for two instances of the Number class.) However, what seems intuitive for one programmer may not be so intuitive for another - some have argued that << and >> are not "intuitive" I/O operators. Nevertheless, if you keep that ultimate goal in sight, your overloaded operators will be better in the long run and should be acceptable to other programmers.
This chapter also introduces a new keyword: friend
. friend
functions blur the line between member and non-member functions:
friend functions
object.function();
class_name::
Friend functions are like a trusted neighbor: they are not a member of our family, but we trust them with keys that allow them to enter our (private) house. By trusting our friends, they can carry out tasks on our behalf (e.g., bringing our mail while we are on vacation) that they could not do otherwise. Similarly, friend functions can perform tasks that not even member functions can do.
While overloaded operators are relatively simple, focusing on four concepts will help you understand them more quickly and deeply.
friend
) functionfriend
) functionsThese concepts are described and illustrated in the following sections.
Number Of Implicit Arguments | Number Of Explicit Arguments | Total Number Of Arguments | ||
---|---|---|---|---|
Unary | Member | 1 | 0 | 1 |
friend | 0 | 1 | 1 | |
Binary | Member | 1 | 1 | 2 |
friend | 0 | 2 | 2 |
function(param1, param2, ...) { ... }
op1 + op2
*x
x * y
this
pointer: x.function(y)
; only member functions have implicit argumentsfunction(x,y)
friend
(i.e. non-member) Functions