Online Book Reader

Home Category

Professional C__ - Marc Gregoire [334]

By Root 1272 0
and anotherIntGrid is Grid. You cannot store SpreadsheetCells or ChessPieces in these grids; the compiler will generate an error if you try to do so.

The type specification is important; neither of the following two lines compiles:

Grid test; // WILL NOT COMPILE

Grid<> test; // WILL NOT COMPILE

The first causes your compiler to complain with something like, “use of class template requires template argument list.” The second causes it to say something like, “wrong number of template arguments.”

If you want to declare a function or method that takes a Grid object, you must specify the type stored in that grid as part of the Grid type:

void processIntGrid(Grid& inGrid)

{

// Body omitted for brevity

}

Code snippet from Grid\GridTest.cpp

Instead of writing the full Grid type everytime, for example Grid, you can use a typedef to give it an easier name:

typedef Grid IntGrid;

Now you can write code as follows:

void processIntGrid(IntGrid& inGrid) { }

The Grid template can store more than just ints. For example, you can instantiate a Grid that stores SpreadsheetCells:

Grid mySpreadsheet;

SpreadsheetCell myCell;

mySpreadsheet.setElementAt(3, 4, myCell);

Code snippet from Grid\GridTest.cpp

You can store pointer types as well:

Grid myStringGrid;

myStringGrid.setElementAt(2, 2, "hello");

Code snippet from Grid\GridTest.cpp

The type specified can even be another template type. The following example uses the vector template from the standard template library (introduced in Chapter 1):

Grid> gridOfVectors;

vector myVector;

gridOfVectors.setElementAt(5, 6, myVector);

Code snippet from Grid\GridTest.cpp

You can also dynamically allocate Grid template instantiations on the heap:

Grid* myGridp = new Grid(); // creates Grid with default width/ height

myGridp->setElementAt(0, 0, 10);

int x = myGridp->getElementAt(0, 0);

delete myGridp;

Code snippet from Grid\GridTest.cpp

How the Compiler Processes Templates

In order to understand the intricacies of templates, you need to learn how the compiler processes template code. When the compiler encounters template method definitions, it performs syntax checking, but doesn’t actually compile the templates. It can’t compile template definitions because it doesn’t know for which types they will be used. It’s impossible for a compiler to generate code for something like x = y without knowing the types of x and y.

When the compiler encounters an instantiation of the template, such as Grid myIntGrid, it writes code for an int version of the Grid template by replacing each T in the template class definition with int. When the compiler encounters a different instantiation of the template, such as Grid mySpreadsheet, it writes another version of the Grid class for SpreadsheetCells. The compiler just writes the code that you would write if you didn’t have template support in the language and had to write separate classes for each element type. There’s no magic here; templates just automate an annoying process. If you don’t instantiate a class template for any types in your program, then the class method definitions are never compiled.

This instantiation process explains why you need to use the Grid syntax in various places in your definition. When the compiler instantiates the template for a particular type, such as int, it replaces T with int, so that Grid is the type.

Selective Instantiation

The compiler generates code only for the class methods that you actually call for a particular type. For example, given the preceding Grid template class, suppose that you write this code (and only this code) in main():

Grid myIntGrid;

myIntGrid.setElementAt(0, 0, 10);

The compiler generates only the 0-argument constructor, the destructor, and the setElementAt() method for an int version of the Grid. It does not generate other methods like the copy constructor, the assignment operator, or getHeight().

Template Requirements on Types

When you write code that

Return Main Page Previous Page Next Page

®Online Book Reader