beautypg.com

Intrusive lists in templates – HP Integrity NonStop J-Series User Manual

Page 115

background image

RWTValDlist myBirthdayV;

gives you a value-based list, where the values are of type pointer to

RWDate

. The collection class

will concern itself only with the pointers, never worrying about the actual RWDate objects they
refer to. Now consider:

RWDate* d1 = new RWDate(29,12,55); // December 29, 1955
myBirthdayV.insert(d1);
RWDate* d2 = new RWDate(29,12,55); // Different object, same date
cout << myBirthdayV.occurrencesOf(d2); // Prints 0

The above code prints 0 because the memory locations of the two date objects are different, and the
collection class is comparing only the values of the pointers themselves (their addresses) when
determining the number of occurrences.

Contrast that with the following:

RWTPtrDlist myBirthdayP; // pointer-based list of RWDates
RWDate* d1 = new RWDate(29,12,55); // December 29,1955
myBirthdayP.insert(d1);
RWDate* d2 = new RWDate(29,12,55); // Different object, same date
cout << myBirthdayP.occurrencesOf(d2); // Prints 1

Here the collection class is parameterized by

RWDate

, not RWDate*, showing that only RWDate

objects, not pointers, are of interest to the list. But because it is a pointer-based collection class,
communicating objects of interest is done via pointers to those objects. The collection class knows
it must dereference these pointers, as well as those stored in the collection class, before comparing
for equality.

Intrusive Lists in Templates

For a collection class of type-name T, intrusive lists are lists where type T inherits directly from the
link type itself

[13]

. The results are optimal in space and time, but require you to honor the

inheritance hierarchy. The disadvantage is that the inheritance hierarchy is inflexible, making it
slightly more difficult to use with an existing class. For each intrusive list class, Tools.h++ offers
templatized value lists as alternative non-intrusive linked lists.

Note that when you insert an item into an intrusive list, the actual item , not a copy, is inserted.
Because each item carries only one link field, the same item cannot be inserted into more than one
list, nor can it be inserted into the same list more than once.

This manual is related to the following products: