Tools.h++ Manual
104011 Tandem Computers Incorporated 16-7
16
Finally, there are two operators for retrieval into a reference to a
RWCollectable
pointer:
RWvistream& operator>>(RWvistream&, RWCollectable*&
obj); // 7
RWFile& operator>>(RWFile&, RWCollectable*&
obj); // 8
As mentioned in the previous section, these last two operators allocate an object
off the heap, restore into it, then return a reference to the resultant pointer. You
are responsible for deleting this object when done. In “Example” on page 2,
why did we chose to use function 2, instead of 7? That is, why say
RWpistream istr(cin);
RWBinaryTree bt2;
istr >> bt2;
...
bt2.clearAndDestroy();
instead of
RWpistream istr(cin);
RWBinaryTree* pTree;
istr >> pTree;
...
pTree->clearAndDestroy();
delete pTree;
The answer is that we could have done it either way. However, if you know
the type of the object, then you are usually better off allocating it yourself, then
restoring via function 2. The reason is that it takes time for the persistence
machinery to figure out the type of object and have the factory (see “An aside:
the RWFactory” on page 6 in Chapter 17, “Designing an RWCollectable Class”)
allocate one. Furthermore, you can tailor the allocation to suite your needs.
For example, you might decide to set an initial capacity for a collection class.