Online Book Reader

Home Category

Professional C__ - Marc Gregoire [318]

By Root 1568 0

{

SpreadsheetCell oldCell(*this); // Save the current value before decrementing

set(mValue - 1); // Decrement

return oldCell; // Return the old value.

}

Code snippet from ArithmeticOperators\SpreadsheetCell.cpp

Now you can increment and decrement your SpreadsheetCell objects to your heart’s content:

SpreadsheetCell c1(4);

SpreadsheetCell c2(4);

c1++;

++c2;

Code snippet from ArithmeticOperators\SpreadsheetCellTest.cpp

Increment and decrement also work on pointers. When you write classes that are smart pointers or iterators, you can overload operator++ and operator-- to provide pointer incrementing and decrementing. This topic is covered in Chapter 17 in the context of writing your own STL iterator.

OVERLOADING THE BITWISE AND BINARY LOGICAL OPERATORS


The bitwise operators are similar to the arithmetic operators, and the bitwise shorthand assignment operators are similar to the arithmetic shorthand assignment operators. However, they are significantly less common, so we do not show examples here. The table in the “Summary of Overloadable Operators” section shows sample prototypes, so you should be able to implement them easily if the need ever arises.

The logical operators are trickier. We don’t recommend overloading && and ||. These operators don’t really apply to individual types: They aggregate results of Boolean expressions. Additionally, you lose the short-circuit evaluation, because both the left-hand side and the right-hand side have to be evaluated before they can be bound to the parameters of your overloaded operator && and ||. Thus, it rarely makes sense to overload them for specific types.

OVERLOADING THE INSERTION AND EXTRACTION OPERATORS


In C++, you use operators not only for arithmetic operations, but also for reading from and writing to streams. For example, when you write ints and strings to cout you use the insertion operator, <<:

int number = 10;

cout << "The number is " << number << endl;

Code snippet from StreamOperators\SpreadsheetCellTest.cpp

When you read from streams you use the extraction operator, >>:

int number;

string str;

cin >> number >> str;

Code snippet from StreamOperators\SpreadsheetCellTest.cpp

You can write insertion and extraction operators that work on your classes as well, so that you can read and write them like this:

SpreadsheetCell myCell, anotherCell, aThirdCell;

cin >> myCell >> anotherCell >> aThirdCell;

cout << myCell << " " << anotherCell << " " << aThirdCell << endl;

Code snippet from StreamOperators\SpreadsheetCellTest.cpp

Before you write the insertion and extraction operators, you need to decide how you want to stream your class out and how you want to read it in. In this example, the SpreadsheetCells will read and write strings.

The object on the left of an insertion or extraction operator is the istream or ostream (such as cin or cout), not a SpreadsheetCell object. Because you can’t add a method to the istream or ostream class, you should write the insertion and extraction operators as global friend functions of the SpreadsheetCell class. The declaration of these functions in your SpreadsheetCell class looks like this:

class SpreadsheetCell

{

public:

// Omitted for brevity

friend ostream& operator<<(ostream& ostr, const SpreadsheetCell& cell);

friend istream& operator>>(istream& istr, SpreadsheetCell& cell);

// Omitted for brevity

};

Code snippet from StreamOperators\SpreadsheetCell.h

By making the insertion operator take a reference to an ostream as its first parameter, you allow it to be used for file output streams, string output streams, cout, cerr, and clog. See Chapter 15 for details. Similarly, by making the extraction operator take a reference to an istream, you can make it work on file input streams, string input streams, and cin.

The second parameter to operator<< and operator>> is a reference to the SpreadsheetCell object that you want to write or read. The insertion operator doesn’t change the SpreadsheetCell it writes, so that reference can be const. The extraction operator, however, modifies the

Return Main Page Previous Page Next Page

®Online Book Reader