beautypg.com

Add definitions for virtual functions – HP Integrity NonStop J-Series User Manual

Page 220

background image

Add Definitions for Virtual Functions

Class

RWCollectable

declares the following virtual functions:

virtual ~RWCollectable();
virtual Rwspace binaryStoreSize() const;
virtual int compareTo(const RWCollectable*) const;
virtual unsigned hash() const;
virtual RWClassID isA() const;
virtual RWBoolean isEqual(const RWCollectable*) const;
virtual RWCollectable* newSpecies() const;
virtual void restoreGuts(RWvistream&);
virtual void restoreGuts(RWFile&);
virtual void saveGuts(RWvostream&) const;
virtual void saveGuts(RWFile&) const;

In these functions RWBoolean is a typedef for an int, RWspace is a typedef for unsigned long, and
RWClassID is a typedef for an unsigned short. Any class that derives from class

RWCollectable

should be able to understand any of these methods. Although default definitions are given for all of
them in the base class RWCollectable, it is best for you as the class designer to provide definitions
tailored to the class at hand.

We've split our discussion of these virtual functions. We discuss the destructor

below (

Object

Destruction

)

, and the binaryStoreSize(), saveGuts(), and restoreGuts() functions

below (

How to Add

Polymorphic Persistence

)

, where we describe how to add persistence to a class. Virtual functions isA()

and newSpecies() are declared and defined by macros, so they were discussed above, in

Virtual

Function isA()

and

Virtual Function newSpecies()

. This section presents discussion on the

remaining functions: compareTo(), isEqual(), and hash(). A very brief example, showing how all
three functions deal with the same data, appears

below (

An example of compareTo()...

)

.

Virtual Function compareTo()

The virtual function compareTo() is used to order objects relative to each other. This function is
required in collection classes that depend on such ordering, such as

RWBinaryTree

or

RWBTree

.

Here is its declaration:

virtual int compareTo(const RWCollectable*) const;

The function int compareTo(const RWCollectable*) const should return a number greater than zero
if self is greater than the argument, a number less than zero if self is less than the argument, and
zero if self is equal to the argument.

The definition and meaning of whether one object is greater than, less than, or equal to another
object is left to the class designer. The default definition, found in class

RWCollectable

, is to

compare the two addresses of the objects. This default definition should be considered a
placeholder; in practice, it is not very useful and could vary from run to run of a program.

Here is a possible definition of compareTo():

This manual is related to the following products: