|Ada 95 Quality and Style Guide||Chapter 5|
5.7.4 Overloaded Operators
Preserve the conventional meaning of overloaded operators (Nissen and Wallis 1984).
Use "+" to identify adding, joining, increasing, and enhancing kinds of functions.
Use "-" to identify subtraction, separation, decreasing, and depleting kinds of functions.
Use operator overloading sparingly and uniformly when applied to tagged types.
examplefunction "+" (X : in Matrix; Y : in Matrix) return Matrix; ... Sum := A + B;
Subverting the conventional interpretation of operators leads to confusing code.
The advantage of operator overloading is that the code can become more clear and written more compactly (and readably) when it is used. This can make the semantics simple and natural. However, it can be easy to misunderstand the meaning of an overloaded operator, especially when applied to descendants. This is especially true if the programmer has not applied natural semantics. Thus, do not use overloading if it cannot be used uniformly and if it is easily misunderstood.
There are potential problems with any overloading. For example, if there are several versions of the "+" operator and a change to one of them affects the number or order of its parameters, locating the occurrences that must be changed can be difficult.
|< Previous Page||Search||Contents||Index||Next Page >|