Online Book Reader

Home Category

Professional C__ - Marc Gregoire [176]

By Root 1231 0
the library by writing your own allocators, algorithms, containers, iterators, and using iterator adapters.

Despite the depth of material found in this and the next chapters, the standard library is too large for this book to cover exhaustively. You should read this and the following chapters to learn about the standard library and the STL, but keep in mind that they don’t mention every method and member that the various classes provide, or show you the prototypes of every algorithm. Appendix C provides a summary of all the header files in the standard library, while the Standard Library Reference resource on the website (www.wrox.com) presents a reference for the various classes and algorithms in the STL.

CODING PRINCIPLES


The standard library, and definitely the standard template library, make heavy use of the C++ features called templates and operator overloading. This section will give a brief introduction to both concepts.

Use of Templates

Templates are described in full detail in Chapter 19. This section will give a basic overview of how to use templates to avoid any problems with code samples. Templates are used to allow generic programming. They make it possible to write code that can work with all kinds of objects, even objects unknown to the programmer when writing the code. The obligation of the programmer writing the template code is to specify the requirements of the classes that define these objects, for example, that they have an operator for comparison, or a copy constructor, or whatever is deemed appropriate, and then making sure the code that is written uses only those required capabilities. The obligation of the programmer creating the objects is to supply those operators and methods that the template writer requires.

Unfortunately, many programmers consider templates to be the most difficult part of C++ and, for that reason, tend to avoid them. However, even if you never write your own templates, you need to understand their syntax and capabilities in order to use the STL.

Explaining the use of templates is best done with an example. Suppose that you would like to write a small class that wraps a standard fixed-sized array of elements of type int. The class definition could be as follows:

class MyArray

{

public:

MyArray(size_t size);

virtual ~MyArray();

size_t getSize() const;

int& at(size_t index) throw(out_of_range);

protected:

size_t mSize;

int* mArray;

};

Code snippet from MyIntArray\MyIntArray.cpp

The implementation is pretty straightforward. Note that error checking is omitted to keep the example compact.

MyArray::MyArray(size_t size) : mSize(size), mArray(nullptr)

{

mArray = new int[size];

}

MyArray::~MyArray()

{

if (mArray) {

delete [] mArray;

mArray = nullptr;

}

}

size_t MyArray::getSize() const

{

return mSize;

}

int& MyArray::at(size_t index) throw(out_of_range)

{

if (index >= 0 && index < getSize())

return mArray[index];

else

throw out_of_range("MyArray::at: Index out of range.");

}

Code snippet from MyIntArray\MyIntArray.cpp

The class defines a constructor that accepts the size of the wrapped array. The constructor allocates the memory for the array while the destructor frees the memory. The getSize() method allows you to query the size of the wrapped array. The class also includes an at() method to give you access to elements of the wrapped array. Note that the at() method returns a reference, so it can also be used to assign values to array elements like in the following example:

MyArray arr(10);

cout << "Array size: " << arr.getSize() << endl;

arr.at(3) = 42;

cout << "arr[3] = " << arr.at(3) << endl;

try {

arr.at(10) = 3;

} catch (const exception& e) {

cout << "Caught exception: '" << e.what() << "'" << endl;

}

Code snippet from MyIntArray\MyIntArray.cpp

Now that you have this class, what would you do when you need to wrap an array of type double, or float or maybe even std::string? One possible solution would be to copy/paste the preceding code and edit it so that the type name is changed to type double, float, or whichever

Return Main Page Previous Page Next Page

®Online Book Reader