www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/12/13/13:42:09

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Inheritance
Date: Thu, 12 Dec 1996 19:39:29 -0800
Organization: Alcyone Systems
Lines: 57
Message-ID: <32B0CFF1.4DEE49BA@alcyone.com>
References: <01bbe870$347e6360$3089d2cd AT inventor DOT worldchat DOT com>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019