Online Book Reader

Home Category

Professional C__ - Marc Gregoire [174]

By Root 1307 0

GameBoard::GameBoard(int inWidth, int inHeight) :

mWidth(inWidth), mHeight(inHeight)

{

mCells = new GamePiece* [mWidth];

for (int i = 0; i < mWidth; i++) {

mCells[i] = new GamePiece[mHeight];

}

}

GameBoard::GameBoard(const GameBoard& src)

{

copyFrom(src);

}

GameBoard::~GameBoard()

{

// Free the old memory

for (int i = 0; i < mWidth; i++) {

delete [] mCells[i];

}

delete [] mCells;

mCells = nullptr;

}

void GameBoard::copyFrom(const GameBoard& src)

{

mWidth = src.mWidth;

mHeight = src.mHeight;

mCells = new GamePiece* [mWidth];

for (int i = 0; i < mWidth; i++) {

mCells[i] = new GamePiece[mHeight];

}

for (int i = 0; i < mWidth; i++) {

for (int j = 0; j < mHeight; j++) {

mCells[i][j] = src.mCells[i][j];

}

}

}

GameBoard& GameBoard::operator=(const GameBoard& rhs)

{

// Check for self-assignment

if (this == &rhs) {

return *this;

}

// Free the old memory

for (int i = 0; i < mWidth; i++) {

delete [] mCells[i];

}

delete [] mCells;

mCells = nullptr;

// Copy the new memory

copyFrom(rhs);

return *this;

}

void GameBoard::setPieceAt(int x, int y, const GamePiece& inElem)

{

mCells[x][y] = inElem;

}

GamePiece& GameBoard::getPieceAt(int x, int y)

{

return mCells[x][y];

}

const GamePiece& GameBoard::getPieceAt(int x, int y) const

{

return mCells[x][y];

}

Code snippet from GameBoard\NoExceptions\GameBoard.cpp

Now, let’s retrofit the preceding class to include error handling and exceptions. The constructors, operator= and copyFrom() can all throw bad_alloc because they perform memory allocation. The destructor, getHeight(), and getWidth() throw no exceptions. setPieceAt() and getPieceAt() throw out_of_range if the caller supplies an invalid coordinate. Here is the retrofitted class definition:

#include

#include

using std::bad_alloc;

using std::out_of_range;

class GameBoard

{

public:

GameBoard(int inWidth = kDefaultWidth,

int inHeight = kDefaultHeight) throw(bad_alloc);

GameBoard(const GameBoard& src) throw(bad_alloc);

virtual ~GameBoard() noexcept;

GameBoard& operator=(const GameBoard& rhs) throw(bad_alloc);

void setPieceAt(int x, int y, const GamePiece& inPiece)

throw(out_of_range);

GamePiece& getPieceAt(int x, int y) throw(out_of_range);

const GamePiece& getPieceAt(int x, int y) const

throw(out_of_range);

int getHeight() const noexcept { return mHeight; }

int getWidth() const noexcept { return mWidth; }

static const int kDefaultWidth = 100;

static const int kDefaultHeight = 100;

protected:

void copyFrom(const GameBoard& src) throw(bad_alloc);

GamePiece** mCells;

int mWidth, mHeight;

};

Code snippet from GameBoard\GameBoard.h

Here are the implementations of the constructor, copyFrom(), and setPieceAt() methods with exception handling. getPieceAt() is not shown because it is similar to setPieceAt().The implementations of the copy constructor and operator= did not change except for their throw lists because all the work is in copyFrom(), so their implementations are not shown. The destructor also did not change, so its implementation is not shown either.

GameBoard::GameBoard(int inWidth, int inHeight) throw(bad_alloc) :

mWidth(inWidth), mHeight(inHeight)

{

int i = 0;

mCells = new GamePiece* [mWidth];

try {

for (i = 0; i < mWidth; i++) {

mCells[i] = new GamePiece[mHeight];

}

} catch (...) {

// Cleanup any memory we already allocated, because the destructor

// will never get called. The upper bound of the for loop is the

// index of the last element in the mCells array that we tried

// to allocate (the one that failed). All indices before that

// one store pointers to allocated memory that must be freed.

for (int j = 0; j < i; j++) {

delete [] mCells[j];

}

delete [] mCells;

mCells = nullptr;

// Translate any exception to bad_alloc.

throw bad_alloc();

}

}

void GameBoard::copyFrom(const GameBoard& src) throw(bad_alloc)

{

int i = 0;

mWidth = src.mWidth;

mHeight = src.mHeight;

mCells = new GamePiece *[mWidth];

try {

for (i = 0; i < mWidth; i++) {

mCells[i] = new GamePiece[mHeight];

}

} catch (...) {

//

Return Main Page Previous Page Next Page

®Online Book Reader