Template classes allow a generic class to be defined. Individual objects can be declared from it by filling in type information in the individual declarations. The classic template example is the array-class. Once the array class template is defined somewhere, various types of arrays can be declared:
// an array of ints: Array<int> ArrayOfInts; // an array of doubles: Array<double> ArrayOfDoubles;The compiler will generate the code needed for access and initialization of each of the above arrays. If the compiler/linker can optimize, it will automatically remove similar code from different modules. Be aware that each of these Array declarations will produce its own code. This may be more code then you might want. There are ways around this, but that is the general limitation of templates.Virtual functions can be seen as alternatives to templates. While templates provide generic source code, virtual functions provide generic object code. Where templates are generally faster, virtual functions generally produce less code. This trade off is not always true, and there are other disadvantages on both sides. I have found however that virtual functions and templates often complement one another, and that very good solutions can be found in the mixture of the two. In the above example one might define:
// an array of anything derived from Numerics: Array<Numerics *> ArrayOfNumbers;Then if Numerics is used as a base class, anything derived from Numerics can be used in this array, and Numerics provides the least-common-denominator for operations available for ArrayOfNumbers.