Online Book Reader

Home Category

Professional C__ - Marc Gregoire [177]

By Root 1543 0
type you need.

By now you should immediately think that this cannot be the best solution to this problem and you are right. Templates allow you to solve exactly this problem without writing any new classes. The only thing you need to do is rewrite the preceding class a little so that it becomes a generic template class that you will be able to use with any type you want. What you have to do is specify that the type of the objects is also a parameter to the specification. This is done with a special syntax. Only a few small changes are required, as shown in the following new class definition. Note also that templates require you to put the implementation of the methods in the header file itself, because the compiler needs to know the complete definition, including the definition of methods before it can create an instance of the template. Putting implementations in header files is only acceptable with templates. In all other cases, it’s highly recommended to put implementation details in source files, not in header files. Chapters 19 and 20 will go much deeper into all the details about templates.

#include

template

class MyArray

{

public:

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

{

mArray = new T[size];

}

virtual ~MyArray()

{

if (mArray) {

delete [] mArray;

mArray = nullptr;

}

}

size_t getSize() const { return mSize; }

T& at(size_t index) throw(std::out_of_range)

{

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

return mArray[index];

else

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

}

protected:

size_t mSize;

T* mArray;

};

Code snippet from TemplateIntroduction\TemplateIntroduction.h

As you can see in the preceding code, instead of using a specific type such as int, an unspecified type T is used everywhere. Before the class keyword, it is stated that this is a template class that accepts just one type, T. You can use any name you want for T, as long as it is not an existing keyword or type.

This generic fixed sized array template class can be used as follows:

// Wrap an int array

MyArray arrInt(10);

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

arrInt.at(3) = 42;

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

try {

arrInt.at(10) = 3;

} catch (const exception& e) {

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

}

// Wrap a std::string array

MyArray arrString(5);

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

arrString.at(3) = "This is a test";

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

try {

arrString.at(10) = 3;

} catch (const exception& e) {

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

}

Code snippet from TemplateIntroduction\TemplateIntroductionTest.cpp

The example code first runs a number of tests on an array of type int, which is created as follows:

MyArray arrInt(10);

With the preceding line, the compiler will instantiate a version of the MyArray class where T is substituted by int. Basically, the compiler will copy all the code for the MyArray class and replace every T with int.

After that, the example runs the same number of tests but on an array with elements of type std::string, which is created as follows:

MyArray arrString(5);

With this line, the compiler will create a second copy of the MyArray code and will replace every T with string.

As you can see, the template parameter between the angled brackets is different. Other than that, using the two arrays is completely the same, independent of the type of the array elements. The output of the preceding example is as follows:

Array size: 10

arr[3] = 42

Caught exception: 'MyArray::at: Index out of range.'

Array size: 5

arr[3] = This is a test

Caught exception: 'MyArray::at: Index out of range.'

Use of Operator Overloading

Operator overloading is used extensively by the C++ standard library, including the STL. Chapter 7 has a whole section devoted to operator overloading. Make sure you read that section and understand it before tackling this and subsequent chapters. In addition, Chapter 18

Return Main Page Previous Page Next Page

®Online Book Reader