Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Essential Tools Module User's Guide

6.7 Collection Class Templates

One of the primary attractions of the C++ language is the reuse of components. The Essential Tools Module collection classes take advantage of several C++ language features that support reuse.

The Smalltalk-like collection classes, discussed in detail in Section 6.19, effect object-code reuse through polymorphism and inheritance. In this chapter, we demonstrate reuse in the Essential Tools Module collection class templates, also referred to simply as templates.

A template is a class declaration parameterized on a type: a prescription for how a particular type of collection class should behave. For example, a Vector template would describe such things as how to index an element, how long it is, how to resize it, and so on. The actual type of the elements is independent of these larger, more general issues.

With templates, you achieve extreme source-code reuse by writing code for your collections without regard to the particular type or types of elements being collected. The template mechanism allows you to represent these types using formal template parameters, which act as place holders. Later, when you want to make use of your collection class template, you instantiate the template with an actual type. At that point, the compiler goes back to the class template and fills it in with that type, as if you wrote it that way to begin with.

Without templates, you would have to write class VectorOfInt, VectorOfDouble, VectorOfFoo, and so on; with templates, you simply code one class, Vector<T>, where T can stand for any type. From there, you're free to create Vector<int>, Vector<double>, Vector<Foo>, or a vector of some type never conceived of when the class template was written originally.



Previous fileTop of DocumentContentsIndex pageNext file

©2004 Copyright Quovadx, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Quovadx, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.