Operator Overloading

In computer programming, operator overloading (less commonly known as operator ad-hoc polymorphism) is a specific case of polymorphism in which some or all of operators like +, =, or == have different implementations depending on the types of their arguments. Sometimes the overloadings are defined by the language; sometimes the programmer can implement support for new types.

Operator overloading is useful because it allows the developer to program using notation closer to the target domain and allows user types to look like types built into the language. It can easily be emulated using function calls; for an example, consider the integers a, b, c:

a + b * c

In a language that supports operator overloading, and assuming the '*' operator has higher precedence than '+', this is effectively a more concise way of writing:

add (a, multiply (b,c))

However, in C++ templates or even C macros, operator overloading is needed in writing down generic, primitive operations such as summing elements when implementing a vector template. The only way to write primitive addition is a + b and this works for all types of elements only because of its overloading. (Actually, in C++ one could define an overloaded function add to use instead, but not in C.)

A more general variant of operator overloading, called expression reduction, allows expressions containing one or multiple operators to be reduced to a single function call. The expression above could be reduced to:

operator_add_and_multiply_integers(a, b, c)

Example

In this case, the addition operator is overloaded to allow addition on a user-defined type "Date" (in C++):

Date Date::operator+(int Month, int Day, int Year)
{
Date temp;
temp.Year += Year;
temp.Day += Day;
if temp.Day > 31
{
temp.Day - 31;
temp.Month + 1;
}
temp.Month += Month;
if temp.Month > 12
{
temp.Month - 12;
temp.Year + 1;
}
}

Addition is a binary operation, which means it has a left and right operand. In C++, the temp object in this case is the left operand and the arguments being passed are the right operands. Note that a unary operator would receive no arguments since it doesn't have any operands.

Criticisms

Operator overloading has often been criticized because it allows programmers to give operators completely different semantics depending on the types of their operands. For example the use of the << in C++'s:

a << 1

shifts the bits in the variable a left by 1 bit if a is of an integer type, but if a is an output stream then the above code will attempt to write a "1" to the stream. Because operator overloading allows the original programmer to change the usual semantics of an operator and to catch any subsequent programmers by surprise, it is usually considered good practice to use operator overloading with care.

Changing the semantics can even happen by accident, as a common pitfall in C++ operator overloading is confusion between the arithmetic operators and assignment operators: Beginners frequently overload the addition (+) operator but give it the semantics of the assignment by addition (+=) operator, resulting in simple expressions like a + b unexpectedly modifying a.

The common reply to this criticism, given by programmers who favor operator overloading, is that the same argument applies to function overloading as well. Furthermore, even in the absence of overloading, a programmer can define a function to do something totally different from what would be expected from its name. An issue that remains is that languages such as C++ provide a limited set of operator symbols, thus removing from programmers the option of choosing a more suitable operator symbol for their new operation.

Another, more subtle issue with operators is that certain rules from mathematics can be expected or unintentionally assumed. For example the commutativity of + (i.e. that a + b == b + a) does not always apply; for example when the operands are strings (i.e. "school" + "bag" is different from "bag" + "school"). A typical counter to this argument comes directly from mathematics: While + is commutative on Integers (and in general any Ring), it is not commutative for other "types" of variable. It can be further noted that + is not even commutative on floating point values in practice due to rounding errors.

Operators are overloaded so that the objects behave as primitive types. New operators cannot be created, only the functionality of existing operators on objects can be modified; at least in C++.

A particular problem from the aspect of performance, is that operator overloading can describe nothing about the relationships between the operators. For instance, it is the case that for an unsigned integer x, the expressions x * 4, x + x + x + x, and x <<> are equivalent, and a compiler can use this equivalence to select the most efficient for every occurrence of the expressions. In contrast, were x an object of a hypothetical Integer class, these expressions would necessarily be output verbatim. As a realistic example, the matrix expression A * B + C has optimization potential that cannot be realized with the straightforward multiply-then-add technique that is the result of overloaded * and + operators. Such optimization opportunities and more can be harnessed explicitly using techniques like expression templates in C++.

0 comments:

About this blog