HELP with C++

gopunk

Lifer
Jul 7, 2001
29,239
2
0
i'm pretty sure i know the answer, but i just wanted to be sure.

ap = new NonmovingObj(animPtr, new Disk(20, Green), 0, 0);
ap->update();


Disk is NOT a subclass of NonmovingObj.

both classes, NonmovingObj, and Disk have a function called update(). does the second line (through dynamic dispatching) call NonmovingObj's or Disk's update function?

i think it's Disk's but i'm not sure.
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
without seeing the code inside the functions i would say no, ap is a pointer to a NonmovingObj type it will call that class' update function.

 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
a case where poly morphisim would come into play is when your update functions were declared virtual and Disk was derived from NonmovingObj and Disk didnt have an update function.

if you made a Disk obj and then called update is would call up the object to the next super class with a update function.
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
hmm, yea pure, now that you put it that way, i see the logic in that.

ameesh, by "no", do you mean that it will NOT call Disk's function?
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0


<< hmm, yea pure, now that you put it that way, i see the logic in that.

ameesh, by "no", do you mean that it will NOT call Disk's function?
>>



correct, it will call Nonmoving obj's function