Operator Overloading Fundamentals


Defining the meaning of an operator relative to a class is called operator overloading. The mechanics of the operator is under the control of the application and the interpretation of each operator may differ from class to class.

Operator overloading allows classes to be defined with a full set of operators - just like the built in classes.

To overload an operator, the operator keyword is used to define the operator method. There are two forms of operator methods, unary operator methods and binary operator methods. The general form of each of these is shown below.

operator op()   // unary operator
{
 // logic goes here
}

operator op(operand)   // binary operator
{
 // logic goes here
}

The operator that is being overloaded (e.g. +, * or /) is substituted for op. The return type is most often the same type as the class for which the operator is being overloaded.