The comparision operator can be overloaded to compare two different types or to compare two equal types that are not basic types. The result type of a comparision operator is always a boolean.
The comparision operators that can be overloaded are:
There is no separate operator for unequal to (<>). To evaluate a statement that contains the unequal to operator, the compiler uses the equal to operator (=), and negates the result.
As an example, the following operator allows to compare two complex numbers:
the above definition allows comparisions of the following form:
The comparision operator definition needs 2 parameters, with the types that the operator is meant to compare. Here also, the compiler doesn’t apply commutativity: if the two types are different, then it is necessary to define 2 comparision operators.
In the case of complex numbers, it is, for instance necessary to define 2 comparsions: one with the complex type first, and one with the real type first.
Given the definitions
the following two comparisions are possible:
Note that the order of the real and complex type in the two comparisions is reversed.