Choosing Which Persistence Operator to Use
In the second example, the persistence operator restored our collection to a reference to an
RWCollectable:
Rwvistream& operator>>(RWvistream&, RWCollectable&);
instead of to a pointer to a reference to an
RWCollectable:
Rwvistream& operator>>(RWvistream&, RWCollectable*&);
The collection was allocated on the stack:
RWpistream istr(cin);
RWOrdered collection2;
istr >> collection2;
...
collection2.clearAndDestroy();
instead of having operator>>(RWvistream&,RWCollectable*&) allocate the memory for the collection:
RWpistream istr(cin);
RWOrdered* pCollection2;
istr >> pCollection2;
...
collection->clearAndDestroy();
delete pCollection2;
Why make this choice? If you know the type of the collection you are restoring, then you are usually better off allocating it yourself, then restoring via:
Rwvistream& operator>>(RWvistream&, RWCollectable&);
By using the reference operator, you eliminate the time required for the persistence machinery to figure out the type of object and have
RWFactory allocate one (see
A Note on the RWFactory.). Furthermore, by allocating the collection yourself, you can tailor the allocation to suit your needs. For example, you can decide to set an initial capacity for a collection class.