Online Book Reader

Home Category

Professional C__ - Marc Gregoire [175]

By Root 1058 0
Clean up any memory we already allocated.

// If this function is called from the copy constructor,

// the destructor will never be called.

// Use the same loop upper bound as described in the constructor.

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

delete [] mCells[j];

}

delete [] mCells;

// Set mCells and mWidth to values that will allow the

// destructor to run without harming anything.

// This function is called from operator=, in which case the

// object was already constructed, so the destructor will be

// called at some point.

mCells = nullptr;

mWidth = 0;

throw bad_alloc();

}

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

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

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

}

}

}

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

throw(out_of_range)

{

// Check for out of range arguments

if (x < 0)

throw out_of_range("GameBoard::setPieceAt: x-coord negative");

if (x >= mWidth)

throw out_of_range("GameBoard::setPieceAt: x-coord beyond width");

if (y < 0)

throw out_of_range("GameBoard::setPieceAt: y-coord negative");

if (y >= mHeight)

throw out_of_range("GameBoard::setPieceAt: y-coord beyond height");

mCells[x][y] = inElem;

}

Code snippet from GameBoard\GameBoard.cpp

SUMMARY


This chapter described the issues related to error handling in C++ programs, and emphasized that you must design and code your programs with an error-handling plan. By reading this chapter, you learned the details of C++ exceptions syntax and behavior. The chapter also covered some of the areas in which error handling plays a large role, including I/O streams, memory allocation, constructors, and destructors. Finally, you saw an example of error handling in a GameBoard class.

Classes and functionality of the C++ standard library have already been used extensively throughout this book, so it’s time to go deeper in on that subject. The next few chapters start delving into the C++ standard library.

Chapter 11

Delving into the Standard Library


WHAT’S IN THIS CHAPTER?

What the coding principles used throughout the Standard Library are

What kind of functionality the Standard Library provides

The most important library that you will use as a C++ programmer is the C++ standard library. As its name implies, this library is part of the C++ standard, so any standards-conforming compiler should include it. The standard library is not monolithic: It includes several disparate components, some of which you have been using already. You may even have assumed they were part of the core language.

The heart of the C++ standard library is its generic containers and algorithms. This subset of the library is often called the Standard Template Library, or STL for short, because of its abundant use of templates. The power of the STL is that it provides generic containers and generic algorithms in such a way that most of the algorithms work on most of the containers, no matter what type of data the containers store. Performance is a very important part of the STL. The goal is to make the STL containers and algorithms as fast as or faster than hand written code.

Many programmers who claim to know C++ have never heard of the Standard Template Library. A C++ programmer who wishes to claim language expertise is expected to be familiar with the Standard Template Library. You can save yourself immeasurable time and energy by incorporating the STL containers and algorithms into your programs instead of writing and debugging your own versions. Now is the time to master the standard library.

This first chapter on the standard library starts with a brief explanation on how to use templates and mentions the concept of operator overloading. Both of these concepts are used extensively by the STL. It also provides a general overview of the functionality available in the standard library and in the STL.

The next few chapters go into much more detail on several aspects of the standard library and the STL, including containers, iterators, generic algorithms, predefined function object classes; and customizing and extending

Return Main Page Previous Page Next Page

®Online Book Reader