beautypg.com

Storage methods of collection classes – HP Integrity NonStop J-Series User Manual

Page 101

background image

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

©Copyright 1996 Rogue Wave Software

Storage Methods of Collection Classes

The general objective of collection classes, called collections for short, is to store and retrieve
objects. In fact, you can classify collection classes according to how they store objects.
Value-based collections store the object itself; reference-based collections store a pointer or
reference to the object. The difference between the two will influence how you use some
features of collection classes in Tools.h++.

Value-based collection classes are simpler to understand and manipulate. You create a linked
list of integers or doubles, for example, or a hash table of shorts. Stored types can be more
complicated, like the

RWCString

s, but the important point is that they act just like values, even

though they may contain pointers to other objects. When an object is inserted into a value-based
collection class, a copy is made. The procedure is similar to C's pass-by-value semantics in
function calls.

In a reference-based collection class, you store and retrieve pointers to other objects. For
example, you could create a linked list of pointers to integers or doubles, or a hash table of
pointers to

RWCString

s.

Let's look at two code fragments that demonstrate the difference between the value-based and
the reference-based procedures:

Value-based Example

Reference-based Example

/* A vector of RWCStrings: */
RWTValOrderedVector
v;
RWCString s("A string");
v.insert(s);

/* A vector of pointers to
RWCStrings: */
RWTPtreOrderedVector
v;
RWCString* p = new RWCString("A
string");
v.insert(p);

Both code fragments insert an

RWCString

into vector v. In the first example, s is an RWCString

object containing "A string". The statement v.insert(s) copies the value of s into the vector. The
object that lies within the vector is distinct and separate from the original object s. In the second
example, p is a pointer to the RWCString object. When the procedure v.insert(p) is complete,
the new element in v will refer to the same RWCString object as p.

This manual is related to the following products: