Online Book Reader

Home Category

Professional C__ - Marc Gregoire [315]

By Root 1542 0
although you can write the operator for whichever types you want, the choice is usually constrained by the class for which you are writing the operator. For example, if you want to implement addition for class T, you wouldn’t write an operator+ that takes two strings! The real choice arises when you try to determine whether to take parameters by value or by reference, and whether or not to make them const.

The choice of value vs. reference is easy: You should take every parameter by reference. As Chapters 7 and 9 explain, never pass objects by value if you can pass-by-reference instead!

The const decision is also trivial: Mark every parameter const unless you actually modify it. The table in the “Summary of Overloadable Operators” section shows sample prototypes for each operator, with the arguments marked const and reference as appropriate.

Choosing Return Types

C++ doesn’t determine overload resolution based on return type. Thus, you can specify any return type you want when you write overloaded operators. However, just because you can do something doesn’t mean you should do it. This flexibility implies that you could write confusing code in which comparison operators return pointers, and arithmetic operators return bools. However, you shouldn’t do that. Instead, you should write your overloaded operators such that they return the same types as the operators do for the built-in types. If you write a comparison operator, return a bool. If you write an arithmetic operator, return an object representing the result of the arithmetic. Sometimes the return type is not obvious at first. For example, as Chapter 6 mentions, operator= should return a reference to the object on which it’s called in order to support nested assignments. Other operators have similarly tricky return types, all of which are summarized in the table in the “Summary of Overloadable Operators” section.

The same choices of reference and const apply to return types as well. However, for return values, the choices are more difficult. The general rule for value or reference is to return a reference if you can; otherwise, return a value. How do you know when you can return a reference? This choice applies only to operators that return objects: The choice is moot for the comparison operators that return bool; the conversion operators that have no return type; and the function call operator, which may return any type you want. If your operator constructs a new object, then you must return that new object by value. If it does not construct a new object, you can return a reference to the object on which the operator is called, or one of its arguments. The table in the “Summary of Overloadable Operators” section shows examples.

A return value that can be modified as an lvalue (the left-hand side of an assignment expression) must be non-const. Otherwise, it should be const. More operators than you might think at first require that you return lvalues, including all of the assignment operators (operator=, operator+=, operator-=, etc.).

If you are in doubt about the appropriate return type, consult the table in the “Summary of Overloadable Operators” section.

Choosing Behavior

You can provide whichever implementation you want in an overloaded operator. For example, you could write an operator+ that launches a game of Scrabble. However, as Chapter 4 describes, you should generally constrain your implementations to provide behaviors that clients expect. Write operator+ so that it performs addition, or something like addition, such as string concatenation.

This chapter explains how you should implement your overloaded operators. In exceptional circumstances, you might want to differ from these recommendations, but, in general, you should follow the standard patterns.

Operators You Shouldn’t Overload

Some operators should not be overloaded, even though it is permitted. Specifically, the address-of operator (operator&) is not particularly useful to overload, and leads to confusion if you do because you are changing fundamental language behavior (taking addresses

Return Main Page Previous Page Next Page

®Online Book Reader