Online Book Reader

Home Category

Professional C__ - Marc Gregoire [319]

By Root 1517 0
SpreadsheetCell object, requiring the argument to be a non-const reference.

Both operators return a reference to the stream they were given as their first argument so that calls to the operator can be nested. Remember that the operator syntax is shorthand for calling the global operator>> or operator<< functions explicitly. Consider this line:

cin >> myCell >> anotherCell >> aThirdCell;

It’s actually shorthand for this line:

operator>>(operator>>(operator>>(cin, myCell), anotherCell), aThirdCell);

As you can see, the return value of the first call to operator>> is used as input to the next. Thus, you must return the stream reference so that it can be used in the next nested call. Otherwise, the nesting won’t compile.

Here are the implementations for operator<< and operator>> for the SpreadsheetCell class:

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

{

ostr << cell.mString;

return ostr;

}

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

{

string temp;

istr >> temp;

cell.set(temp);

return istr;

}

Code snippet from StreamOperators\SpreadsheetCell.cpp

The trickiest part of these functions is that, in order for mValue to be set correctly, operator>> must remember to call the set() method on the SpreadsheetCell instead of setting mString directly.

OVERLOADING THE SUBSCRIPTING OPERATOR


Pretend for a few minutes that you have never heard of the vector template class in the STL, and so you have decided to write your own dynamically allocated array class. This class would allow you to set and retrieve elements at specified indices, and would take care of all memory allocation “behind the scenes.” A first stab at the class definition for a dynamically allocated integer array might look like this:

class Array

{

public:

// Creates an array with a default size that will grow as needed.

Array();

virtual ~Array();

// Returns the value at index x. If index x does not exist in the array,

// throws an exception of type out_of_range.

int getElementAt(size_t x) const;

// Sets the value at index x to val. If index x is out of range,

// allocates more space to make it in range.

void setElementAt(size_t x, int val);

protected:

static const size_t kAllocSize = 4;

void resize(size_t newSize);

// Sets all elements to 0

void initializeElements();

int* mElems;

size_t mSize;

private:

// Disallow assignment and pass-by-value

Array(const Array& src);

Array& operator=(const Array& rhs);

};

Code snippet from SubscriptOperator\Array.h

In order to present only the salient points, we have omitted exception throw lists and have not made this class a template. The interface supports setting and accessing elements. It provides random-access guarantees: A client could create an array and set elements 1, 100, and 1000 without worrying about memory management.

Here are the implementations of the methods:

Array::Array()

{

mSize = kAllocSize;

mElems = new int[mSize];

initializeElements();

}

Array::~Array()

{

delete [] mElems;

mElems = nullptr;

}

void Array::initializeElements()

{

for (size_t i = 0; i < mSize; i++)

mElems[i] = 0;

}

void Array::resize(size_t newSize)

{

// Make a copy of the current elements pointer and size

int* oldElems = mElems;

size_t oldSize = mSize;

// Create new bigger array

mSize = newSize; // store the new size

mElems = new int[newSize]; // Allocate the new array of the new size

initializeElements(); // Initialize all elements to 0

// The new size is always bigger than the old size

for (size_t i = 0; i < oldSize; i++) {

// Copy the elements from the old array to the new one

mElems[i] = oldElems[i];

}

delete [] oldElems; // free the memory for the old array

}

int Array::getElementAt(size_t x) const

{

if (x < 0 || x >= mSize) {

throw out_of_range("");

}

return mElems[x];

}

void Array::setElementAt(size_t x, int val)

{

if (x < 0) {

throw out_of_range("");

}

if (x >= mSize) {

// Allocate kAllocSize past the element the client wants

resize(x + kAllocSize);

}

mElems[x] = val;

}

Code snippet from SubscriptOperator\Array.cpp

Return Main Page Previous Page Next Page

®Online Book Reader