Mail Archives: djgpp/1996/12/13/13:42:09
Vincent Collura wrote:
> And all the other 3 types of classes can use this pointer, however in
> djgpp you cannot do this.
This is not a limitation in DJGPP (or gcc/g++); this is a draft standard
C++ limitation.
Basically what you're trying to do is something contrary to the design of
C++ (more below).
> Base *ClassPoint[100] and that works however I cannot access the memeber
> functions in the derived classes. Can someone please help me.
There are a few things you can do.
The first is to simply have an array of void *, and then cast the instances
appropriately. This assumes that you know what these objects' real classes
are _before_ you come across them.
Your second option, which is done by basically everyone in a similar
situation (in one way or another*), is this: Every pointer of to a derived
class can be casted without worry to a pointer to its base class. Further,
if you _know_ what the real derived class is, you can cast it from the a
base class pointer back to the actual derived class pointer without worry.
So what you do is you have a virtual member function in the base class,
which returns an indication (be it an integer, an enum, a string, whatever)
that indicates what it is. This member function is overridden in the
derived classes appropriately. Thus you can store them in a big array of
pointers to the base class, call this member function to see what type it
"really is," and then cast it before you want to do your
derived-class-specific operations on it. If you want more explanation of
this I can give examples.
However, I must emphasize that this is generally considered pretty ugly
(though it works just fine); it's somewhat missing the point of using
object-oriented class hierarchies. The idea is that the member function
which does the work (whatever the class-specific operations) are virtual in
the base class and overridden in the derived classes. This way things are
all done automatically with one call to the appropriate virtual member
function.
-----
* The new draft standard C++ specification has a feature which will do this
for you: RTTI. However, I don't know if RTTI support is in gcc 2.7.2.x,
so I won't go into it.
--
Erik Max Francis | max AT alcyone DOT com
Alcyone Systems | http://www.alcyone.com/max/
San Jose, California | 37 20 07 N 121 53 38 W
&tSftDotIotE | R^4: the 4th R is respect
"But since when can wounded eyes see | If we weren't who we were"
- Raw text -