[gameprogrammer] Re: Pure virtual function getting called

On Thu, 20 May 2004 07:34:05 +0100, kevin-shea@xxxxxxxx said:
> I seen to remember running into this problem at one point and
>  believe its "undefined behaviour" according to the standard,
> i.e. compiler-dependent. Could be wrong though :)
>
> A simple alternative might be to store the class name in the
> base class and pass in its value via the constructor. Get's
> you past calling a virtual function at the cost of allocating
> a string for every class instance.

Presumably you wouldn't even have that cost, as you'd just need to pass
the char * to the constructor, and it would be stored in the executable
so there would be no cost.  I think that's the most straightforward
option.  It would be nicer if it was automatic, but in the absence of
that it's fairly easy to just pass the name to the constructor.

class Base
{
public:
  Base(const char* iClassName):
    mClassName(iClassName)
  {}

  const char* getClassName() const { return mClassName; }
private:
  const char* const mClassName;
};

class Derived : public Base
{
public:
  Derived():
    Base("Derived")
  {}
};

The user can't _forget_ to pass something to the Base constructor, but
there is the dual maintenance problem of having the class name
duplicated.  Still, in the absence of a perfect heavy-duty template and
RTTI solution, it looks OK to me.

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


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


Other related posts: