title: Rewriting class from VFTable c++
body: <p>I am trying to rewrite a class from VFTable, I was able to </p>
  
  <ol>
  <li>Get the offset of the VFTable which contains functions pointers</li>
  <li>Get a pointer of the desired class instance at runtime</li>
  </ol>
  
  <p>What I am trying to achieve is to write the class in my own c++ code with the ability to execute the vftable functions whenever I need. I can parse all the functions inculded in the VFTable in the same order to keep the padding but what is the correct syntax to make sure my class calls the vftable functions?</p>
  
  <p>Let's assume the following code is compiled and I do not have access to it</p>
  
  <pre><code>class BaseA
  {
      virtual void BaseA1Function(int x, int y);
      virtual int BaseA2Function();
  }
  
  class BaseB
  {
      virtual void BaseB1Function(int z);
      virtual int BaseB2Function();
  }
  
  class MainClass : BaseA, BaseB
  {
      int someVariable;
  }
  
  class Other
  {
      MainClass* GetBySomeVariable(int _someVariable)//I hooked this
      {
          //returns MainClass pointer;
      }
  }
  </code></pre>
  
  <p>With my understanding of old compilers vftable creation is that each class will have it's own vftable assuming MainClass has it's own vftable that has pointers to each function of base classes vftable functions.</p>
  
  <p>I was able to hook <strong>GetBySomeVariable()</strong> and get instance of MainClass in my own code as following</p>
  
  <pre><code>class MyMainClass
  {
      //I would like to rewrite functions here that can call the vftables functions
      //0 BaseA1Function
      //4 BaseA2Function
      //8 BaseB1Function
      //12 BaseB2Function
  }
  
  void main()
  {
      MyMainClass* _instance = //hooking Other::GetBySomeVariable(123);
  }
  </code></pre>
  
  <p>May someone shed some light?</p>