Tools.h++ Manual

104011 Tandem Computers Incorporated 17-13
17
would be disastrous as we would have to dereference
passengers_
:
strm << *passengers_;
Instead, we pass it to
RWvostream& operator<<(RWvostream&, const
RWCollectable*);
which automatically detects the nil pointer and stores a record of it.
Multiply-referenced objects
Note – a passenger name can exist in the set pointed to by
customers_
and in
the set pointed to by
passengers_
. That is, both collections contain the same
string. When the
Bus
is restored, we want to make sure that this relationship
is maintained.
We don’t have to do anything! Consider the call:
Bus aBus;
RWFile aFile(“busdata.dat”);
aBus.addPassenger(“John”);
aFile << aBus;
Note – the name “John” will be on both the customer list and the passenger
list. Note that both lists will be saved in this single call: first the customer list,
then the passenger list. The persistence machinery will save the first reference
to “John”, but for the second reference it will merely store a reference to the
first copy. During the restore (see below), both references will resolve to the
same object, replicating the original morphology of the collection.