Tools.h++ Manual

104011 Tandem Computers Incorporated 12-5
12
Although shallow copies can be useful (and fast, because less copying is done),
one must be careful because two collections now reference the same object. If
you delete all the items in one collection, you will leave the other collection
pointing into nonsense.
The issue of shallow versus deep copies can also arise when an object is
written to disk. If an object includes two or more pointers or references to the
same object, then when it is restored it is important that this morphology be
preserved. Classes which inherit from
RWCollectable
inherit algorithms
which guarantee to preserve an object's morphology. More on this later.
Value-basedcollections
Now contrast this situation with a value-based collection. For the sake of
definiteness, consider the class
RWTValOrderedVector<RWCString>
, that is,
an ordered vector template, instantiated for
RWCString
's. In this case, each
string is embedded within the collection. When a copy of the collection is
made, not only is the collection itself copied, but also the objects in it. This
results in distinct new copies of the collected objects:
Figure 12-3 Coping a value-based collection
Copy
Indian
Arctic
Southern
Pacific
Indian
Arctic
Southern
Pacific
Indian
Arctic
Southern
Pacific