jonnytheclown said:
What is the correct (recommended) way to inherit from an arbitrary
class when I know nothing about its internals (and don't want to know)
but need to hold additional instance variables. The next question is
how should I implement a class that allows other programmers to inherit
from it and hold their own instance variables.
Let me answer the second question first.
The rule is simple: To make a class easy to inherit from, identify
(document) a set of accessors. The set will usually include a creator
method (->new), methods that read or set object variables, plus class
methods to control class variables if such are used. The set should be
kept as small as reasonably possible.
Then write all other methods in terms of these. No exceptions, no cutting
corners.
If a class is written this way, a derived class must define (override)
exactly the accessor methods to guarantee that all methods of the base
class will work with derived objects. It is free to add its own accessors
to additional object (or class-) variables.
To inherit from an arbitrary class that doesn't define such a set of
accessors, you will have to take a look at the implementation to identify
an equivalent set (i.e. a set of methods all others are defined in terms
of). Use that as described above. If you're unlucky, the set will be
large -- it may comprise all methods for an exceptionally badly written
class. In that case, the class can't be usefully inherited from.
Anno