title: How does the compiler know which entry in vtable corresponds to a virtual function?
body: <p>Let's say we have more than one virtual function in the parent class and derived class. There will be a vtable created for these virtual functions in the vtable for both the parent derived class.</p>
  
  <p>How will the compiler know which entry in the vtable correspond to which virtual function?</p>
  
  <p>Example:</p>
  
  <pre><code>class Animal{
  public:
   void fakeMethod1(){}
   virtual void getWeight(){}
   void fakeMethod2(){}
   virtual void getHeight(){}
   virtual void getType(){}
  };
  
  class Tiger:public Animal{
  public:
   void fakeMethod3(){}
   virtual void getWeight(){}
   void fakeMethod4(){}
   virtual void getHeight(){}
   virtual void getType(){}
  };
  main(){
  Animal a* = new Tiger();
  a-&gt;getHeight(); // A  will now point to the base address of vtable Tiger
  //How will the compiler know which entry in the vtable corresponds to the function getHeight()?
  }
  </code></pre>
  
  <p>I have not found exact explanation in my research -</p>
  
  <p><a href="https://stackoverflow.com/a/99341/437894">https://stackoverflow.com/a/99341/437894</a> = </p>
  
  <blockquote>
    <p>"This table is used to resolve the function call as it contains the
    addresses of all the virtual functions of that class."</p>
  </blockquote>
  
  <p>How exactly is the table used to resolve the function call?</p>
  
  <p><a href="https://stackoverflow.com/a/203136/437894">https://stackoverflow.com/a/203136/437894</a> =</p>
  
  <blockquote>
    <p>"So at runtime, the code just uses the object's vptr to locate the
    vtbl, and from there the address of the actual overridden function."</p>
  </blockquote>
  
  <p>I am not able to understand this. Vtable holds the address of the virtual function not the address of actual overridden function.</p>