Restrictions on Operator Overloading

Most of C++'s operators can be overloaded. These are shown in Fig. 11.1. Figure 11.2 shows the operators that cannot be overloaded.

Figure 11.1. Operators that can be overloaded.

Operators that can be overloaded

       

+

-

*

/

%

^

&

|

~

!

=

<

>

+=

-=

*=

/=

%=

^=

&=

|=

<<

>>

>>=

<<=

==

!=

<=

>=

&&

||

++

--

->*

,

->

[]

()

new

delete

new[]

delete[]

Figure 11.2. Operators that cannot be overloaded.

Operators that cannot be overloaded

.

.*

::

?:

Common Programming Error 11.1

Attempting to overload a nonoverloadable operator is a syntax error.

 

Precedence, Associativity and Number of Operands

The precedence of an operator cannot be changed by overloading. This can lead to awkward situations in which an operator is overloaded in a manner for which its fixed precedence is inappropriate. However, parentheses can be used to force the order of evaluation of overloaded operators in an expression.


The associativity of an operator (i.e., whether the operator is applied right-to-left or left-to-right) cannot be changed by overloading.

It is not possible to change the "arity" of an operator (i.e., the number of operands an operator takes): 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; these unary and binary versions can each be overloaded.

Common Programming Error 11.2

Attempting to change the "arity" of an operator via operator overloading is a compilation error.

 

Creating New Operators

It is not possible to create new operators; only existing operators can be overloaded. Unfortunately, this prevents the programmer from using popular notations like the ** operator used in some other programming languages for exponentiation. [Note: You could overload the ^ operator to perform exponentiationas it does in some other languages.]

Common Programming Error 11.3

Attempting to create new operators via operator overloading is a syntax error.

 

Operators for Fundamental Types

The meaning of how an operator works on objects of fundamental types cannot be changed by operator overloading. The programmer cannot, for example, change the meaning of how + adds two integers. 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.

Software Engineering Observation 11.2

At least one argument of an operator function must be an object or reference of a user-defined type. This prevents programmers from changing how operators work on fundamental types.

Common Programming Error 11.4

Attempting to modify how an operator works with objects of fundamental types is a compilation error.

 

Related Operators

Overloading an assignment operator and an addition operator to allow statements like

object2 = object2 + object1;

does not imply that the += operator is also overloaded to allow statements such as

object2 += object1;

Such behavior can be achieved only by explicitly overloading operator += for that class.

Common Programming Error 11.5

Assuming that overloading an operator such as + overloads related operators such as += or that overloading == overloads a related operator like != can lead to errors. Operators can be overloaded only explicitly; there is no implicit overloading.

Категории