10.3 Fundamentals of Operator Overloading

As you saw in Fig. 10.1, overloaded operators provide a concise notation for manipulating string objects. You can use operators with your own user-defined types as well. Although C++ does not allow new operators to be created, it does allow most existing operators to be overloaded so that, when they’re used with objects, they have meaning appropriate to those objects.

10.3.1 Operator Overloading Is Not Automatic

You must write operator-overloading functions to perform the desired operations. An operator is overloaded by writing a non-static member function definition or non-member function definition as you normally would, except that the function name starts with the keyword operator followed by the symbol for the operator being overloaded. For example, the function name operator+ would be used to overload the addition operator (+) for use with objects of a particular user-defined type. When operators are overloaded as member functions, they must be non-static, because they must be called on an object of the class and operate on that object.

10.3.2 Operators That You Do Not Have to Overload

To use an operator on an object of a class, you must define overloaded operator functions for that class—with three exceptions:

  • The assignment operator (=) may be used with most classes to perform memberwise assignment of the data members—each data member is assigned from the assignment’s “source” object (on the right) to the “target” object (on the left). Memberwise assignment is dangerous for classes with pointer members, so we’ll explicitly overload the assignment operator for such classes. [Note: This is also true of the C++11 move assignment operator, which we discuss in Chapter 24.]

  • The address (&) operator returns a pointer to the object; this operator also can be overloaded.

  • The comma operator evaluates the expression to its left then the expression to its right, and returns the value of the latter expression. This operator also can be overloaded.

10.3.3 Operators That Cannot Be Overloaded

Most of C++’s operators can be overloaded. Figure 10.2 shows the operators that cannot be overloaded.1

Fig. 10.2 Operators that cannot be overloaded.

Operators that cannot be overloaded
. .* (pointer to member) :: ?:

10.3.4 Rules and Restrictions on Operator Overloading

As you prepare to overload operators for your own classes, there are several rules and restrictions you should keep in mind:

  • An operator’s precedence cannot be changed by overloading. Parentheses can be used to force the order of evaluation of overloaded operators in an expression.

  • An operator’s associativity cannot be changed by overloading—if an operator normally associates from left to right, then so do all of its overloaded versions.

  • An operator’s “arity” (that is, the number of operands an operator takes) cannot be changed—overloaded unary operators remain unary operators; overloaded binary operators remain binary operators. C++’s only ternary operator, ?:, cannot be overloaded. Operators &, *, + and - all have both unary and binary versions that can be separately overloaded.

  • Only existing operators can be overloaded—you cannot create new ones.

  • You cannot overload operators to change how an operator works on fundamental-type values. For example, you cannot make the + operator subtract two ints. Operator overloading works only with objects of user-defined types or with a mixture of an object of a user-defined type and an object of a fundamental type.

  • Related operators, like + and +=, must be overloaded separately.

  • When overloading (), [], -> or any of the assignment operators, the operator overloading function must be declared as a class member. For all other overloadable operators, the operator overloading functions can be member functions or non-member functions.

Software Engineering Observation 10.1

Overload operators for class types so they work as closely as possible to the way built-in operators work on fundamental types.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset