Online Book Reader

Home Category

Professional C__ - Marc Gregoire [101]

By Root 1286 0
SpreadsheetCell(const string& initialValue);

SpreadsheetCell(const SpreadsheetCell& src);

SpreadsheetCell& operator=(const SpreadsheetCell& rhs);

// Remainder omitted for brevity

};

Code snippet from OperatorOverloading\AddSecondAttempt\SpreadsheetCell.h

The explicit keyword goes only in the class definition, and only makes sense when applied to constructors with exactly one argument.

The selection of an implicit constructor might be inefficient, because temporary objects must be created. To avoid implicit construction for adding a double, you could write a second operator+ as follows:

const SpreadsheetCell SpreadsheetCell::operator+(double rhs) const

{

return SpreadsheetCell(mValue + rhs);

}

Code snippet from OperatorOverloading\AddSecondAttempt\SpreadsheetCell.cpp

Note also that this demonstrates that you don’t need to create a variable to return a value.

Third Attempt: Global operator+

Implicit conversions allow you to use an operator+ method to add your SpreadsheetCell objects to ints and doubles. However, the operator is not commutative, as shown in the following code:

aThirdCell = myCell + 4; // Works fine.

aThirdCell = myCell + 5.6; // Works fine.

aThirdCell = 4 + myCell; // FAILS TO COMPILE!

aThirdCell = 5.6 + myCell; // FAILS TO COMPILE!

Code snippet from OperatorOverloading\AddSecondAttempt\SpreadsheetCellTest.cpp

The implicit conversion works fine when the SpreadsheetCell object is on the left of the operator, but doesn’t work when it’s on the right. Addition is supposed to be commutative, so something is wrong here. The problem is that the operator+ method must be called on a SpreadsheetCell object, and that object must be on the left-hand side of the operator+. That’s just the way the C++ language is defined. So, there’s no way you can get the above code to work with an operator+ method.

However, you can get it to work if you replace the in-class operator+ with a global operator+ function that is not tied to any particular object. The function looks like this:

const SpreadsheetCell operator+(const SpreadsheetCell& lhs,

const SpreadsheetCell& rhs)

{

SpreadsheetCell newCell;

newCell.set(lhs.mValue + rhs.mValue); // update mValue and mString.

return newCell;

}

Code snippet from OperatorOverloading\SpreadsheetCell.cpp

Now all four of the addition lines work as you expect:

aThirdCell = myCell + 4; // Works fine.

aThirdCell = myCell + 5.6; // Works fine.

aThirdCell = 4 + myCell; // Works fine.

aThirdCell = 5.6 + myCell; // Works fine.

Code snippet from OperatorOverloading\SpreadsheetCellTest.cpp

Note that the implementation of the global operator+ accesses protected data members of SpreadsheetCell objects. Therefore, it must be a friend function of the SpreadsheetCell class:

class SpreadsheetCell

{

public:

// Omitted for brevity

friend const SpreadsheetCell operator+(const SpreadsheetCell& lhs,

const SpreadsheetCell& rhs);

//Omitted for brevity

};

Code snippet from OperatorOverloading\SpreadsheetCell.h

You might be wondering what happens if you write the following code:

aThirdCell = 4.5 + 5.5;

Code snippet from OperatorOverloading\SpreadsheetCellTest.cpp

It compiles and runs, but it’s not calling the operator+ you wrote. It does normal double addition of 4.5 and 5.5, which results in the following intermediate statement:

aThirdCell = 10;

To make this assignment work, there should be a SpreadsheetCell object on the right-hand side. The compiler will discover a user-defined constructor that takes a double, will use this constructor to implicitly convert the double value into a temporary SpreadsheetCell object, and will then call the assignment operator.

Overloading Arithmetic Operators

Now that you understand how to write operator+, the rest of the basic arithmetic operators are straightforward. Here are declarations of -, *, and / (you can also overload %, but it doesn’t make sense for the double values stored in SpreadsheetCells):

class SpreadsheetCell

{

public:

// Omitted for brevity

friend const SpreadsheetCell operator+(const SpreadsheetCell&

Return Main Page Previous Page Next Page

®Online Book Reader