[comp.sys.mac.programmer] C++ method overriding

aries@rhi.hi.is (Mimir Reynisson) (02/26/91)

Ok, ok, I know Bjarne and his bunch don't want to call methods
methods, but methinks methods are methods and should be slightly
different than functions. Which brings me to the point.

Consider the following example:

class someClass : public HandleObject {

  public:

    someClass(void);

    short getSomeID(void);

};

class someSubClass : public someClass {

  public:

   short getSomeID(void);

};

someClass::someClass(void) {

  CreateSomething(getSomeID());
};

someClass:getSomeID(void) {

  return kSomething;
};

someSubClass::getSomeID(void) {

  return kSomethingElse;
};

Now in the above example someClass::someClass() doesn't call
someSubClass::getSomeID(). No thank you very much, it calls
someClass:getSomeID() - which is plain silly.

I presume I'll have to say this->getSomeID() to get the
right method, right???

Can anybody confirm this correct? Or I'm just being a dum doik?

--MymR
On the subject of disclaimers:
  Q: What do you call ten thousand dead lawyers at the bottom of the ocean? 
  A: A good start...

ml27192@uxa.cso.uiuc.edu (lanett mark) (02/26/91)

aries@rhi.hi.is (Mimir Reynisson) writes:

[stuff deleted]
>Now in the above example someClass::someClass() doesn't call
>someSubClass::getSomeID(). No thank you very much, it calls
>someClass:getSomeID() - which is plain silly.

>I presume I'll have to say this->getSomeID() to get the
>right method, right???

>Can anybody confirm this correct? Or I'm just being a dum doik?

Constructors always call functions of their own class, even if the functions
are virtual and overloaded. For that matter, since constructors should
be inittializing info, it's not necessarily such a good idea to have them
calling other functions. Just create a new constructor for the subclass and
you'll be set.

--
//-----------------------------------------------------------------------------
Mark Lanett						ml27192@uxa.cs.uiuc.edu
Quote of the Day: "I switched from a Sun 2/3 to a Mac Plus to get some speed"

aries@rhi.hi.is (Mimir Reynisson) (02/27/91)

>Constructors always call functions of their own class, even if the functions
>are virtual and overloaded. For that matter, since constructors should
>be inittializing info, it's not necessarily such a good idea to have them
>calling other functions. Just create a new constructor for the subclass and
>you'll be set.

Yup. I did something like that shortly after I posted my article. What I
was actually doing was plain silly - too little sleep and too much coffee,
I presume.

Anyway, now there is another impending problem. Well it's not actually
a problem, it's rather a nucance.

Look at this code fragment:

doDrawSomething() {

  Rect bounds;

  SetRect(&bounds, 10, 10, 50, 50);  // Draw a rectangle
  FrameRect(&bounds);

  MoveTo(bounds.left+1,bounds.botom); // Shade the rectangle
  LineTo(bounds.right,bounds.bottom);
  LineTo(bounds.right,bounds.top+1);
};

On my planet that does do what it's supposed to do. The shading is not
even near the rectangle let alone in the correct shape. To get the right
shading I'll have to do the following:

doDrawSomething() {
 
  Rect bounds;
  short h,v;

  SetRect(&bounds, 10, 10, 50, 50);  // Draw a rectangle
  FrameRect(&bounds);

  h=bounds.left; 
  MoveTo(h+1,bounds.botom); // Shade the rectangle
  LineTo(bounds.right,bounds.bottom);
  v=bounds.top;
  LineTo(bounds.right,v+1);
};

Or something like this:

doDrawSomething() {
 
  Rect bounds;
  short h,v;
 
  SetRect(&bounds, 10, 10, 50, 50);  // Draw a rectangle
  FrameRect(&bounds);
 
  h=bounds.left+1;
  MoveTo(h,bounds.botom); // Shade the rectangle
  LineTo(bounds.right,bounds.bottom);
  v=bounds.top+1;
  LineTo(bounds.right,v);
};

Can anybody please point out to me what is bogus with my code?

--MymR

aries@rhi.hi.is (Mimir Reynisson) (02/28/91)

In reference to my previous follow-up:

I discovered MPW C 3.2b3a100 to generate incorrect code for the
following example:

#include <Types.h>
main()
{
	Rect	h;

	Check(h.left);                 // this is ok
	Check((h.left + 1));           // this is ok
	Check((short)h.left + 1);      // this is ok
	Check((short)(h.left + 1));    // we get incorrect code here!
}

What I did was to compile this from the shell and then to call dumpobj
to analyze the code. Here is the dump I made:

00000000: 4E56 FFF8      'NV..'            LINK       A6,#$FFF8
00000004: 302E FFFA      '0...'            MOVE.W     -$0006(A6),D0
00000008: 48C0           'H.'              EXT.L      D0
0000000A: 2F00           '/.'              MOVE.L     D0,-(A7)
0000000C: 4EBA 0000      'N...'            JSR        Check               

00000010: 302E FFFA      '0...'            MOVE.W     -$0006(A6),D0
00000014: 48C0           'H.'              EXT.L      D0
00000016: 5280           'R.'              ADDQ.L     #$1,D0
00000018: 2F00           '/.'              MOVE.L     D0,-(A7)
0000001A: 4EBA 0000      'N...'            JSR        Check               

0000001E: 302E FFFA      '0...'            MOVE.W     -$0006(A6),D0
00000022: 48C0           'H.'              EXT.L      D0
00000024: 5280           'R.'              ADDQ.L     #$1,D0
00000026: 2F00           '/.'              MOVE.L     D0,-(A7)
00000028: 4EBA 0000      'N...'            JSR        Check               

Here is the incorrect part:

0000002C: 302E FFFC      '0...'            MOVE.W     -$0004(A6),D0
                                                        ???? wrong member

00000030: 5240           'R@'              ADDQ.W     #$1,D0
00000032: 48C0           'H.'              EXT.L      D0
00000034: 2F00           '/.'              MOVE.L     D0,-(A7)
00000036: 4EBA 0000      'N...'            JSR        Check             

0000003A: 4E5E           'N^'              UNLK       A6
0000003C: 4E75           'Nu'              RTS        
0000003E: 846D 6169      '.mai'            OR.W       $6169(A5),D2
00000042: 6E00 0000      'n...'            BGT        *+$0002            


I think I could live with this situation if it weren't for the fact that
MPW C++ generates C code that looks like:

	MoveTo((short )(h. left +1), (short )h. top);

Which means that C gets it wrong and I have to do my calculations before
calling my function.s If I havn't said it already then I'll do so now:
this only happens in argument section of a function call not in assignments
(although I haven't tried to do an assignment in the argument section ..
um I wonder if that'd work ..)

Anyway there you have it ...

-MymR