[gameprogrammer] Re: Pure virtual function getting called

On Wed, 19 May 2004 18:55:26 -0700, "Kevin Jenkins"
<gameprogrammer@xxxxxxxxxx> said:

G'day Kevin.

> I have this in the base class
> virtual char* GetClassName(void) const=0;
> 
> This in the derived class
> virtual char* GetClassName(void) const {return "Player";}
> 
> Yet when I call GetClassName within the base class constructor, it is
> still calling the base class version of GetClassName and
> complaints about calling a pure virtual function.

Yeah, I couldn't remember the specifics of this but just looked it up on
comp.lang.c++.moderated.

Basically, it is generally legal to call virtual functions from the
constructor or destructor, but the only version of the function you will
get is the one in the current class.  This is because the compiler in
general has no way of knowing whether the other classes are ready for the
call.

So, for the purposes of virtual function resolution, the type of an
object in a constructor is the static type, not the dynamic type as you'd
expect.

> On a similar topic, all GetClassName does is return the name of the
> class it is enclosed in.  I made it pure virtual so the users
> wouldn't forget to define it.  But if there is an automatic way to 
> do this please let me know.

Can't think of anything really nice off the top of my head.

Dave.
-- 
  Dave Slutzkin
  Melbourne, Australia
  daveslutzkin@xxxxxxxxxxx


---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: