Cryptography Reference
In-Depth Information
˜ LINT()
{
delete [] n_l;
}
14.3 Overloaded Operators
The overloading of operators represents a powerful mechanism that makes it
possible to define functions with the same name but with different parameter
lists, functions that can then carry out differing operations. The compiler uses the
specified parameter list to determine which function is actually meant. To make
this possible C++ employs strong type-checking, which tolerates no ambiguity or
inconsistency.
The overloading of operator functions makes it possible to use the “normal”
way of expressing a sum c=a+b with LINT objects a , b , and c instead of having
to invoke a function like, for example, add_l(a_l, b_l, c_l) . This enables the
seamless integration of our class into the programming language and significantly
improves the readability of programs. For this example it is necessary to overload
both the operator “+” and the assignment “=”.
There are only a few operators in C++ that cannot be overloaded. Even the
operator “ [] ”, which is used for access to vectors, can be overloaded, for example
by a function that simultaneously checks whether the access to a vector oversteps
the vector's bounds. However, please note that the overloading of operators opens
the door to all possible mischief. To be sure, the effect of the operators of C++ on
the standard data types cannot be altered; nor can the predefined precedence
order of the operators (cf. [Str1], Section 6.2) be changed or new operators
“created.” But for individual classes it is fully possible to define operator functions
that have nothing in common with what one traditionally has associated with the
operator as it is normally employed. In the interest of maintainability of programs
one is well advised to stick close to the meaning of the standard operators in C++
when overloading operators if one is to avoid unnecessary confusion.
One should note in the above outline of the LINT class that certain operators
have been implemented as friend functions and others as member functions. The
reason for this is that we would like, for example, to use “ + ” or “ * ” as two-position
operators that can not only process two equivalent LINT objects but accept
alternatively one LINT object and one of the built-in C++ integer types, and
moreover, accept the arguments in either order, since addition is commutative. To
this end we require the above-described constructors, which create LINT objects
of out integer types. Mixed expressions such as in
LINT a, b, c;
int number;
 
Search WWH ::




Custom Search