beautypg.com

Template naming convention, Value vs. reference semantics in templates – HP Integrity NonStop J-Series User Manual

Page 114

background image

The preceding example demonstrates the basic operation of templates. Using the collection class
template RWTValDList, we instantiate the object stringList, simply by specifying type RWCString.
The template gives us complete flexibility in specifying the type of the list; we don't write code for
the object, and Tools.h++ doesn't complicate its design with a separate class
RWTValDListofRWCString. Without the template, we would be limited to types provided by the
program, or forced to write the code ourselves.

Template Naming Convention

You'll notice that the collection class template

RWTValDlist

in the example follows a unique

format. In Tools.h++, all templates have class names starting with RWT, for Rogue Wave Template,
followed by a three letter code:

Isv -- Intrusive lists

Val -- Value-based

Ptr -- Pointer-based

Hence,

RWTValOrderedVector

is a value-based template for an ordered vector of type-name T.

RWTPtrMultiMap

is a pointer-based template based on the Standard C++ Library

multimap class. Special characteristics may also modify the name, as in

RWTValSortedDlist

,

a value-based doubly-linked template list that automatically maintains its elements in sorted order.

Value vs. Reference Semantics in Templates

Tools.h++ collection class templates can be either value-based or pointer-based. Value-based
collections use value semantics, maintaining copies of inserted objects and returning copies of
retrieved objects. In contrast, pointer-based collections use reference semantics, dealing with
pointers to objects as opposed to the objects themselves. See

Chapter 10 (

Storage Methods of Collection

Classes

)

for other examples of value and reference semantics.

Templates offer you a choice between value and reference semantics. In fact, in most cases, you
must choose between a value-based or a pointer-based class; for example, either
RWOrderedVectorVal, or RWOrderedVectorPtr.

Your choice depends on the requirements of your application. Pointer-based templates are a good
choice for maximizing efficiency for large objects, or if you need to have the same group of objects
referred to in several ways, requiring that each collection class point to the target objects, rather
than wholly contain them.

An Important Distinction

There is a big difference between a value-based collection of pointers, and a pointer-based
collection class. You can save yourself difficulty by understanding the distinction. For example,
declaring:

// value-based list of RWDate pointers:

This manual is related to the following products: