Online Book Reader

Home Category

Professional C__ - Marc Gregoire [314]

By Root 1332 0
a global overloaded operator function must be a user-defined type (e.g., a class). This means that you can’t do something ridiculous such as redefine + for ints to mean subtraction (though you could do so for your classes). The one exception to this rule is the memory allocation and deallocation routines; you can replace the global routines for all memory allocations in your program.

Some of the operators already mean two different things. For example, the - operator can be used as a binary operator, as in x = y - z; or as a unary operator, as in x = -y;. The * operator can be used for multiplication or for dereferencing a pointer. The << operator is the insertion operator or the left-shift operator, depending on the context. You can overload both meanings of operators with dual meanings.

Choices in Operator Overloading

When you overload an operator, you write a function or method with the name operatorX, where X is the symbol for some operator. For example, Chapter 7 declares operator+ for SpreadsheetCell objects like this:

friend const SpreadsheetCell operator+(const SpreadsheetCell& lhs,

const SpreadsheetCell& rhs);

You are allowed to put whitespace between operator and the X; "operator X" is the same as "operatorX". The following sections describe several choices involved in each overloaded operator function or method you write.

Method or Global Function

First, you must decide whether your operator should be a method of your class or a global function (usually a friend of the class). How do you choose? First, you need to understand the difference between these two choices. When the operator is a method of a class, the left-hand side of the operator expression must always be an object of that class. If you write a global function, the left-hand side can be an object of a different type.

There are three different types of operators:

Operators that must be methods: The C++ language requires some operators to be methods of a class because they don’t make sense outside of a class. For example, operator= is tied so closely to the class that it can’t exist anywhere else. The table in the “Summary of Overloadable Operators” section lists those operators that must be methods. For these, the choice of method or global function is simple. However, most operators do not impose this requirement.

Operators that must be global functions: Whenever you need to allow the left-hand side of the operator to be a variable of a different type than your class, you must make the operator a global function. This rule applies specifically to operator<< and operator>>, where the left-hand side is the iostream object, not an object of your class. Additionally, commutative operators like binary + and - should allow variables that are not objects of your class on the left-hand side. Chapter 7 mentions this problem.

Operators that can be either methods or global functions: There is some disagreement in the C++ community on whether it’s better to write methods or global functions to overload operators. However, we recommend the following rule: Make every operator a method unless you must make it a global function as described previously. One major advantage to this rule is that methods can be virtual, but friend functions cannot. Therefore, when you plan to write overloaded operators in an inheritance tree, you should make them methods if possible.

When you write an overloaded operator as a method, you should mark the entire method const if it doesn’t change the object. That way, it can be called on const objects.

Choosing Argument Types

You are somewhat limited in your choice of argument types because you can’t usually change the number of arguments (although there are exceptions, which are explained later in this chapter). For example, operator+ must always have two arguments if it is a global function; one argument if it’s a method. The compiler issues an error if it differs from this standard. In this sense, the operator functions are different from normal functions, which you can overload with any number of parameters. Additionally,

Return Main Page Previous Page Next Page

®Online Book Reader