Runtime overriding of virtual functions

From: Dim St Thomas (dimstthomas_at_yahoo.com)
Date: 06/07/04


Date: 6 Jun 2004 19:44:18 -0700

Say you have a library with a header file that defines two classes:

class A
{
protected:
  virtual void draw();
};

class B
{
public:
  A *getWidget() { return m_a; }
private:
  A *m_a;
};

Is there any way of creating an instance of class B such that when it
calls the virtual draw function of m_a it will call a user derived
version of draw? The two ways I can think of are making the m_a
variable public and setting it equal to a class derived from A or
somehow modifying the virtual function table of the pointer returned
by getWidget(). I assume these are both hacks that are dependent on
the behaviour of the compiler. Has anyone got this to work using
Visual C++?

Thanks