C++ - When is the vtable pointer (VPTR) updated?

An object's vtable pointer is updated in the constructor member initialization list.


  class MyClass
  {
      MyClass() : // CBase() - base class constructor is called first
          __vfptr(&MyClass::vftable) // compiler generated vtable pointer assignment
      {
            // vtable ptr is valid up to and including this class (MyClass)
            //
            // virtual methods called from the constructor body will execute overloads up to MyClass only
            //
      }
  }
  

Note: Names prefixed with a double underscore are reserved for the compiler's internal use.


Ads by Google


Ask a question, send a comment, or report a problem - click here to contact me.

© Richard McGrath