Online Book Reader

Home Category

Professional C__ - Marc Gregoire [313]

By Root 1138 0
of operators, such as +, -, and =, for your classes. Many object-oriented languages do not provide this capability, so you might be tempted to disregard its usefulness in C++. However, it can be beneficial for making your classes behave similarly to built-in types such as ints and doubles. It is even possible to write classes that look like arrays, functions, or pointers.

Chapters 3 and 4 introduce object-oriented design and operator overloading, respectively. Chapters 6 and 7 present the syntax details for objects and for basic operator overloading. This chapter picks up operator overloading where Chapter 7 left off.

This chapter focuses on the syntax and semantics of operator overloading. Practical examples are provided for most of the operators, but for a few of them, you already saw practical examples elsewhere in this book. This chapter does not repeat information which is in Chapter 7.

OVERVIEW OF OPERATOR OVERLOADING


As Chapter 1 explains, operators in C++ are symbols such as +, <, *, and <<. They work on built-in types such as int and double to allow you to perform arithmetic, logical, and other operations. There are also operators such as -> and * that allow you to dereference pointers. The concept of operators in C++ is broad, and even includes [] (array index), () (function call), casting, and the memory allocation and deallocation routines.

Operator overloading allows you to change the behavior of language operators for your classes. However, this capability comes with rules, limitations, and choices.

Why Overload Operators?

Before learning how to overload operators, you probably want to know why you would ever want to do so. The reasons vary for the different operators, but the general guiding principle is to make your classes behave like built-in types. The closer your classes are to built-in types, the easier they will be for clients to use. For example, if you want to write a class to represent fractions, it’s quite helpful to have the ability to define what +, -, *, and / mean when applied to objects of that class.

The second reason to overload operators is to gain greater control over the behavior in your program. For example, you can overload memory allocation and deallocation routines for your classes to specify exactly how memory should be distributed and reclaimed for each new object.

It’s important to emphasize that operator overloading doesn’t necessarily make things easier for you as the class developer; its main purpose is to make things easier for clients of the class.

Limitations to Operator Overloading

Here is a list of things you cannot do when you overload operators:

You cannot add new operator symbols. You can only redefine the meanings of operators already in the language. The table in the “Summary of Overloadable Operators” section lists all of the operators that you can overload.

There are a few operators that you cannot overload, such as . (member access in an object), :: (scope resolution operator), sizeof, ?: (the ternary operator), and a few others. The table lists all the operators that you can overload. The operators that you can’t overload are usually not those you would care to overload anyway, so we don’t think you’ll find this restriction limiting.

You cannot change the arity of the operator. The arity describes the number of arguments, or operands, associated with the operator. Unary operators, such as ++, work on only one operand. Binary operators, such as +, work on two operands. There is only one ternary operator: ?:. The main place where this limitation might bother you is when overloading [] (array brackets), discussed later in this chapter.

You cannot change the precedence or associativity of the operator. These rules determine in which order operators are evaluated in a statement. Again, this constraint shouldn’t be cause for concern in most programs because there are rarely benefits to changing the order of evaluation.

You cannot redefine operators for built-in types. The operator must be a method in a class, or at least one of the arguments to

Return Main Page Previous Page Next Page

®Online Book Reader