Runtime overriding of virtual functions

From: Dim St Thomas (
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
  virtual void draw();

class B
  A *getWidget() { return m_a; }
  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++?