Deciphering operator overloading essentials
C++ has a variety of operators in the language. C++ allows most operators to be redefined to include usage with user defined types; this is known as operator overloading. In this way, user defined types may utilize the same notation as standard types to perform these well-understood operations. We can view an overloaded operator as polymorphic in that its same form can be used with a variety of types – standard and user defined.
Not all operators may be overloaded in C++. The following operators cannot be overloaded: the member access operator (.
), the ternary conditional operator (?:
), the scope resolution operator (::
), the pointer-to-member operator (.*
), the sizeof()
operator, and the typeid()
operator. All the rest may be overloaded, provided at least one operand is a user defined type.
When overloading an operator, it is important to promote the same meaning that the operator has for standard types. For example, the extraction...