Online Book Reader

Home Category

Professional C__ - Marc Gregoire [316]

By Root 1518 0
of variables) in potentially unexpected ways. The entire STL, which uses operator overloading extensively, never overloads the address-of operator.

Additionally, you should avoid overloading the binary Boolean operators operator&& and operator|| because you lose C++’s short-circuit evaluation rules.

Finally, you should not overload the comma operator (operator,). Yes, you read that correctly: there really is a comma operator in C++. It’s also called the sequencing operator, and is used to separate two expressions in a single statement, while guaranteeing that they are evaluated left to right. There is rarely (if ever) a good reason to overload this operator.

Summary of Overloadable Operators

The following table lists the operators that you can overload, specifies whether they should be methods of the class or global friend functions, summarizes when you should (or should not) overload them, and provides sample prototypes showing the proper return values.

This table should be a useful reference in the future when you want to sit down and write an overloaded operator. You’re bound to forget which return type you should use, and whether or not the function should be a method. We’re looking forward to referring to this table ourselves.

In this table, T is the name of the class for which the overloaded operator is written, and E is a different type (not the name of the class).

Rvalue References

Chapter 9 discusses the new C++11 concept called rvalue references, written as && instead of the normal lvalue references, &. They are demonstrated in Chapter 9 by defining move assignment operators, which are used by the compiler in contexts where the second object is a temporary object that will be destroyed after the assignment. The normal assignment operator from the preceding table has the following prototype:

T& operator=(const T&);

The move assignment operator has almost the same prototype, but uses an rvalue reference. It will modify the argument so it cannot be passed as const. Details are explained in Chapter 9:

T& operator=(T&&);

The preceding table does not include sample prototypes with rvalue reference semantics. However, for most operators it can make sense to write a version using normal lvalue references and a version using rvalue references, but whether it makes sense depends on implementation details of your class. The operator= is one example from Chapter 9. Another example is operator+ to prevent unnecessary memory allocations. The std::string class for example implements an operator+ using rvalue references as follows (simplified):

string operator+(string&& lhs, string&& rhs);

The implementation of this operator will reuse memory of one of the arguments because they are being passed as rvalue references, meaning both are temporary objects that will be destroyed when operator+ is finished. The implementation of the preceding operator+ has the following effect:

return std::move(lhs.append(rhs));

or

return std::move(rhs.insert(0, lhs));

In fact, std::string defines several overloaded operator+ operators with different combinations of lvalue references and rvalue references. The following is a list of all operator+ operators for std::string accepting two strings as arguments (simplified):

string operator+(const string& lhs, const string& rhs);

string operator+(string&& lhs, const string& rhs);

string operator+(const string& lhs, string&& rhs);

string operator+(string&& lhs, string&& rhs);

Reusing memory of one of the rvalue reference arguments is implemented in the same way as it is explained for move assignment operators in Chapter 9.

OVERLOADING THE ARITHMETIC OPERATORS


Chapter 7 shows how to write the binary arithmetic operators and the shorthand arithmetic assignment operators, but it does not cover how to overload the other arithmetic operators.

Overloading Unary Minus and Unary Plus

C++ has several unary arithmetic operators. Two of these are unary minus and unary plus. Here is an example of these operators using ints:

int i, j = 4;

i = -j; // Unary minus

i = +i; // Unary

Return Main Page Previous Page Next Page

®Online Book Reader