[comp.sys.mac.programmer] Think C polymorphism problem

dmmg1176@uxa.cso.uiuc.edu (David M Marcovitz) (03/07/91)

I am using Think C 4.0 with TCL.  I have a superclass CFoo with
subclasses CBar and CBaz.  CFoo has the following methods:

void CFoo::MyMainMethod()
{
   TheAuxMethod();  /* Call an auxiliary method in CBar or CBaz */
}

void CFoo::TheAuxMethod()
{
   /* This is to be overridden by CBar and CBaz */
}
------------------------------------

CBar and CBaz have the following method:

void	CBar::TheAuxMethod()  /*substitute CBaz for CBar in CBaz.c*/
{
    /* Insert stuff that should get done. */
}
------------------------------------

When MyMainMethod is running (a method of CFoo), it calls
TheAuxMethod.  I don't want CFoo to have to know if the real object is
a CBar or CBaz object (i.e., I don't want some if structure to check
if I am a CBar and call CBar::TheAuxMethod or CBaz::TheAuxMethod).

I thought that by calling TheAuxMethod, the correct TheAuxMethod (from
the subclass, CBar or CBaz) would run.  That is not happening.  The
CFoo TheAuxMethod is getting called; it is not being overriden.

How can I call TheAuxMethod from one of CFoo's methods and have it be
overridden by CBar or CBaz's TheAuxMethod?

--
David M. Marcovitz                     |  internet: marcovitz@uiuc.edu
Computer-based Education Research Lab  |            dmmg1176@uxa.cso.uiuc.edu
University of Illinois                 |  novanet:  marco / cca / cerl

dmmg1176@uxa.cso.uiuc.edu (David M Marcovitz) (03/08/91)

dmmg1176@uxa.cso.uiuc.edu (David M Marcovitz) writes:


>I am using Think C 4.0 with TCL.  I have a superclass CFoo with
>subclasses CBar and CBaz.  CFoo has the following methods:

>I thought that by calling TheAuxMethod, the correct TheAuxMethod (from
>the subclass, CBar or CBaz) would run.  That is not happening.  The
>CFoo TheAuxMethod is getting called; it is not being overriden.

>How can I call TheAuxMethod from one of CFoo's methods and have it be
>overridden by CBar or CBaz's TheAuxMethod?

Sorry, it was my fault.  My superclass methods had slightly different
names than my subclass methods.  I changed the names, and it works
now.   Thanks to those who responded.

--
David M. Marcovitz                     |  internet: marcovitz@uiuc.edu
Computer-based Education Research Lab  |            dmmg1176@uxa.cso.uiuc.edu
University of Illinois                 |  novanet:  marco / cca / cerl

emmayche@dhw68k.cts.com (Mark Hartman) (03/17/91)

dmmg1176@uxa.cso.uiuc.edu (David M Marcovitz) writes of a problem where a
method in a superclass wants to call a method which is overridden by a 
subclass; however, the code which winds up getting run is the method that
lives in the superclass.

It would probably help if you would post the exact way that you are calling
the method for which override is not working.  However, I have never had any
problem by using the form

          this->TheAuxMethod();

If you omit the "this->", THINK C believes that you're trying to call the
method from the current class (see page 188; it's stated badly, but you have
to read carefully).

-- 
Mark Hartman, N6BMO           "What are you just standing there for?  Where
Applelink: N1083 or BINARY.TREE      do you think you are, DIS-ney World??"
Internet: emmayche@dhw68k.cts.com                -- General Knowledge, from
uucp: ...{spsd,zardoz,felix}!dhw68k!emmayche                CRANIUM COMMAND