Tools.h++ Manual
17-6 104011 Tandem Computers Incorporated
17
Virtual function newSpecies()
The job of this function is to return a pointer to a brand new object of the same
type as self. The declaration of this function is automatically provided by the
macro
RWDECLARE_COLLECTABLE
(className). The definition is automatically
provided by the macro
RWDEFINE_COLLECTABLE
(className,classID).
An aside: the RWFactory
As mentioned, you must include the macro
RWDEFINE_COLLECTABLE
somewhere in a
.cc
file where it will be compiled. For our example, the macro
might look like this:
RWDEFINE_COLLECTABLE(Bus, 200)
This magic incantation will allow a new instance of your class to be created
given only its
RWClassID
:
Bus* newBus = (Bus*)theFactory->create(200);
This is used internally by the persistence machinery to create a new instance of
your persisted object where its type is not known at runtime. You will not
normally use this capability in your own source code. The pointer
theFactory
is a global pointer that points to a one-of-a-kind global instance
of class
RWFactory
, used to create new instances of any class, given its
RWClassID
. The use of
RWFactory
is generally transparent to the user. See
Part II: Class Reference for more details on
RWFactory
.
17.6 Object destruction
All objects inheriting from class
RWCollectable
inherit a virtual destructor.
Hence, the actual type of the object need not be known until runtime 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 any resources that they hold. In the case of Bus, the
names of passengers and customers are
RWCollectableStrings
that were
allocated off the heap. Hence, they must be reclaimed. Because these strings