dougm@zamenhof.rice.edu (Doug Moore) (06/16/90)
There is a level of data hiding that I feel I ought to be able to
achieve with inheritence, but can't. Perhaps someone can give me some
hints about how I can do this data hiding.
I want to have a main program that uses a type called SplaySet, an
implementation of self-adjusting trees, but I want to be able to
totally disconnect the implementation of SplaySet from the main
program. If I have a header file SplaySet.h that I include in the
main program and that defines the SplaySet class with private members,
I have a sort of data hiding. But, if I decide to change the private
implementation of SplaySet, it requires a change to SplaySet.h and a
recompilation.
I note that I can have a pointer to SplaySet in my main program if I
just put "class SplaySet;" somewhere near the top. I can define it
and pass it, I just can't dereference it without knowing just what a
SplaySet looks like. What I thought would work would be to declare a
base class Set with virtual member functions "add", "del", "contains",
etc. I put the class definition in Set.h and included that in my main
program. Then I declared SplaySet as a derived class of Set. Thus,
the program begins
class Set {
public:
virtual void add(int item) = 0;
etc.
};
class SplaySet : public Set;
So I've promised the compiler that SplaySets have member functions
called add, del, etc. It has all it needs to type check them,
generate mangled names, etc. As long as I only manipulate pointers to
SplaySets and only dereference them into member functions declared
virtual in Set, I should be OK. So I think I've moved the
implementation details of SplaySet completely out of my main program
and left it to the linker to connect things up.
Of course, it doesn't work. Sun C++ calls
class SplaySet : public Set;
a syntax error. g++ accepts it but later rejects my use of the
undefined type SplaySet. I looked over Lippman and couldn't find
anything about whether the line should be legal or not; obviously, I'd
like it to be.
So, can I achieve the level of data hiding I want? And is the line
syntactically correct or not?
Thanks for listening.
Dougm