Tools.h++ Manual
104011 Tandem Computers Incorporated 19-7
19
has already seen this instance before and, rather than call
saveGuts()
again,
will just make a reference to the previously written link. This stops the series
of recursive calls and the stack unwinds.
Restoration of the chain is done in a similar manner. A call to
RWFile& operator>>(RWFile&, RWCollectable*&);
will either create a new object off the heap and return a pointer to it, return the
address of a previously read object, or return the null pointer. In the case of
the last two choices, the recursion stops and the stack unwinds.
19.3 Multiple inheritance
In Chapter 17, “Designing an RWCollectable Class,” we built a
Bus
class by
inheriting from
RWCollectable
. If we had an existing
Bus
class at hand, we
might have been able to use multiple inheritance to create a new class with the
functionality of both
Bus
and
RWCollectable
, perhaps saving ourselves some
work:
class CollectableBus : public RWCollectable, public Bus {
.
.
.
};
This is the approach taken by many of the Tools.h++ collectable classes (e.g.,
class
RWCollectableString
, which inherits both class
RWCollectable
and
class
RWCString
). The general idea is to create your object first, and then tack
on the
RWCollectable
class, making the whole thing collectable. This way,
you will be able to use your objects for other things, in other situations, where
you might not want to inherit from class
RWCollectable
.
There is another good reason for using this approach: avoiding ambiguous
base classes. Here's an example:
class A { };
class B : public A { };
class C : public A { };
class D : public B, public C { };
void fun(A&);