Chapter 9: Operator Overloading
Operator overloading permits us to ascribe new meaning to standard operators. C++ allows both unary and binary operators to be overloaded as either member or non-member functions. It is important to associate an operator with an operator that is consistent with the way the operator is used with other objects.
An operator function can be a non-static member function, or it can be a non-member function having at least one parameter whose type is a class, reference to a class, an enumeration, or a reference to an enumeration.
Whenever possible, operator functions should be class members because operations performed on instances of the class should be encapsulated there. However, if making an operator a member function would result in awkward expressions involving the operator, we make an exception to the rule.
A cast operator is absolutely required when converting from a class type to a basic (predefined) type such as int, float, or double. A constructor with a single argument will always be used by the compiler for implicit conversions.
We created a Time class, in which the unary ++ and -- operators were overloaded, and range checking was performed on minute and hour values.
We showed how overloaded operators could be used to improve the FString class, by allowing the concatenation and assignment of strings.
In this chapter, we explained how the standard bitwise operators work on unsigned integers: << (left shift), >> (right shift), | (bitwise OR), & (bitwise AND), ~ (NOT), and ^ (XOR).
We created the BitArray class, modeled after the <bitset> class in the C++ Draft Standard. This class has operator functions for performing unions, intersections, adding elements, and removing elements.