Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Professional C++ [eng].pdf
Скачиваний:
284
Добавлен:
16.08.2013
Размер:
11.09 Mб
Скачать

Mastering Classes and Objects

set(mValue / rhs.mValue); // Call set to update mValue and mString. return (*this);

}

The shorthand arithmetic operators are combinations of the basic arithmetic and the assignment operators. With the above definitions, you can now write code like this:

SpreadsheetCell myCell(4), aThirdCell(2); aThirdCell -= myCell;

aThirdCell += 5.4;

You cannot, however, write code like this (which is a good thing!):

5.4 += aThirdCell;

Overloading Comparison Operators

The comparison operators, such as >, <, and ==, are another useful set of operators to define for your classes. Like the basic arithmetic operators, they should be global friend functions so that you can use implicit conversion on both the left-hand side and right-hand side of the operator. The comparison operators all return a bool. Of course, you can change the return type, but we don’t recommend it. Here are the declarations and definitions:

class SpreadsheetCell

{

public:

// Omitted for brevity

friend const SpreadsheetCell operator+(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);

friend const SpreadsheetCell operator-(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);

friend const SpreadsheetCell operator*(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);

friend const SpreadsheetCell operator/(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);

SpreadsheetCell& operator+=(const SpreadsheetCell& rhs); SpreadsheetCell& operator-=(const SpreadsheetCell& rhs); SpreadsheetCell& operator*=(const SpreadsheetCell& rhs); SpreadsheetCell& operator/=(const SpreadsheetCell& rhs); friend bool operator==(const SpreadsheetCell& lhs,

const SpreadsheetCell& rhs);

friend bool operator<(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);

friend bool operator>(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);

friend bool operator!=(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);

friend bool operator<=(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);

friend bool operator>=(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);

215

Chapter 9

// Omitted for brevity };

bool operator==(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs)

{

return (lhs.mValue == rhs.mValue);

}

bool operator<(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs)

{

return (lhs.mValue < rhs.mValue);

}

bool operator>(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs)

{

return (lhs.mValue > rhs.mValue);

}

bool operator!=(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs)

{

return (lhs.mValue != rhs.mValue);

}

bool operator<=(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs)

{

return (lhs.mValue <= rhs.mValue);

}

bool operator>=(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs)

{

return (lhs.mValue >= rhs.mValue);

}

In classes with more data members, it might be painful to compare each data member. However, once you’ve implemented == and <, you can write the rest of the comparison operators in terms of those two. For example, here is a definition of operator>= that uses operator<:

bool operator>=(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs)

{

return (!(lhs < rhs));

}

You can use these operators to compare SpreadsheetCells to other SpreadsheetCells, and to doubles and ints:

if (myCell > aThirdCell || myCell < 10) { cout << myCell.getValue() << endl;

}

Building Types with Operator Overloading

Many people find the syntax of operator overloading tricky and confusing, at least at first. The irony is that it’s supposed to make things simpler. As you’ve discovered, that doesn’t mean simpler for the

216