Introduction – HP Integrity NonStop J-Series User Manual

Page 112

Advertising
background image

Click on the banner to return to the user guide home page.

©Copyright 1996 Rogue Wave Software

Introduction

As a developer, you no doubt periodically ask yourself, "Haven't I coded this before?" Clearly,
one of the primary attractions of the C++ language is the promise of reuse, the lure of avoiding
rewrites of the same old code, over and over again. The Tools.h++ collection classes take
advantage of several C++ language features that support reuse.

The Smalltalk-like collection classes, discussed later in detail, effect object-code reuse through
polymorphism and inheritance. In this chapter, we demonstrate reuse in the Tools.h++
collection class templates, called templates in our jargon.

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 had written it that way in the first place.

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.

Advertising
This manual is related to the following products: