beautypg.com

Object destruction, How to add polymorphic persistence, Consider whether you need to define a destructor – HP Integrity NonStop J-Series User Manual

Page 223: See below, To add definitions for the following functions, How to add, Below, Object, Destruction, Polymorphic persistence

background image

lexicographically in a case sensitive manner. See class

RWCString

in the Class Reference for

details.

Object Destruction

All objects inheriting from class

RWCollectable

inherit a virtual destructor. Hence, the actual type

of the object need not be known until run time in order to delete the object. This allows all items in
a collection to be deleted without knowing their actual type.

As with any C++ class, objects inheriting from

RWCollectable

may need a destructor to release the

resources they hold. In the case of Bus, the names of passengers and customers are

RWCollectableString

s that were allocated off the heap. Hence, they must be reclaimed. Because

these strings never appear outside the scope of the class, we do not have to worry about the user
having access to them. Hence, we can confidentially delete them in the destructor, knowing that no
dangling pointers will be left.

Furthermore, because the set pointed to by customers_ is a superset of the set pointed to by
passengers_, it is essential that we delete only the contents of customers_.

Here's a possible definition:

Bus::~Bus()
{ customers_.clearAndDestroy();
delete passengers_;
}

Note that the language guarantees that it is okay to call delete on the pointer passengers_ even if it
is nil.

How to Add Polymorphic Persistence

The saveGuts() and restoreGuts() virtual functions are responsible for saving and restoring the
internal state of

RWCollectable

objects. To add persistence to your RWCollectable class, you must

override the saveGuts() and restoreGuts() virtual member functions so that they write out all of
your object's member data. The next two sections describe approaches you can use to correctly
define these functions.

Multiply Referenced Objects

describes how these functions handle

multiply-referenced objects.

Polymorphically saving an object to a file may require some knowledge of the number of bytes that
need to be allocated for storage of an object. The binaryStoreSize() function calculates this value.

Virtual Function binaryStoreSize()

describes how to use binaryStoreSize().

RWCollection

has its own versions of the saveGuts() and restoreGuts() functions that are used to

polymorphically save collections that inherit from that class.

Polymorphically Persisting Custom

Collections

briefly describes how these functions work.

Virtual Functions saveGuts(RWFile&) and saveGuts(RWvostream&)

This manual is related to the following products: