Deriving Specialized Classes

Deriving Specialized Classes Hierarchy Select a Class

You can derive specialized classes that use keys and store data of any type.

Each node within the tree contains two void* items, one for the node key and the other for the node data. Virtuals are called to compare keys, set and get data, set and get key values, and delete key and data values. When these virtuals are called, they are passed references to the void* items for key or data as appropriate. Your implementation can thus use the void* items to store the address of the key or data, or the value of the key or data, depending on the kind of data that makes up the key or data.

For example, the tULongToULong class stores the value of the unsigned-long key and the unsigned-long data in the appropriate void* references passed to its virtuals; on the other hand, the tStringToULong stores the address of the key and the value of the data.

If you derive a class directly from tBalTree (the abstract base tree class), you must implement overrides for the following pure virtuals:

onDeleteKey() To delete a key value

onDeleteData() To delete a data value

To store a new key value

onSetData() To store a new data value

onCompareKeys() To compare two keys

Additionally, it is recommended that you implement an override for the following member,

onGetKeyName() Used during DumpStructure()

It is important to note that the above pure virtual functions need perform only those operations for which they are specifically called. For example, if a class's implementation stores the address of an object in the data pointer, the class's onSetData() member does not need to delete the existing data before storing a pointer to the new data; the onDeleteData() member will already have been called by the tBalTree class. Likewise the onDeleteData() member does not need to zero the pointer after deleting dynamic data, because onDeleteData() is only called in two cases; immediately before the node itself is destroyed, and immediately before onSetData() is called to update the node's data.

It is also important to note that the virtual destructor of every class derived directly from tBalTree must call the RemoveAll() member, or memory leaks will occur. This is because the tree is emptied only by RemoveAll(), and because RemoveAll() depends on the existence of the pure virtual functions onDeleteData() and onDeleteKey(). The base-class destructor cannot call these pure virtuals because they are no longer valid at the time the base class destructor is called. Always code a virtual destructor that contains a call to RemoveAll().

You should refer to Handles Serialization for additional information before implementing your derived class if you require serialization support.