title: pointer to access member function through virtual pointer
body: <p>I came across articles where in they explain about vptr and vtable.
  I know that the first pointer in an object in case of a class with virtual functions stored, is a vptr to vtable and vtable's array entries are pointers to the function in the same sequence as they occur in class ( which I have verified with my test program).
  But I am trying to understand what syntax must compiler put in order to call the appropriate function.</p>
  
  <p><strong>Example:</strong></p>
  
  <pre><code>class Base
   {
     virtual void func1() 
     { 
         cout &lt;&lt; "Called me" &lt;&lt; endl; 
     }
  };
  int main()
  {
    Base obj;
    Base *ptr;
    ptr=&amp;obj;
  
  // void* is not needed. func1 can be accessed directly with obj or ptr using vptr/vtable
    void* ptrVoid=ptr; 
  
  // I can call the first virtual function in the following way:
    void (*firstfunc)()=(void (*)(void))(*(int*)*(int*)ptrVoid); 
    firstfunc();
  }
  </code></pre>
  
  <p><strong>Questions:</strong></p>
  
  <p><strong>1.</strong> But what I am really trying to understand is how compiler replaces the call to <code>ptr-&gt;func1()</code> with <code>vptr</code>?
  If I were to simulate the call then what should I do? should I overload the <code>-&gt;</code> operator. But even that would not help as I would not know what really the name <code>func1</code> is. Even if they say that compiler accesses the vtable through vptr, still how does it know that the entry of <code>func1</code> is the first array adn entry of <code>func2</code> is the second element in the array? There must be some mapping for the names of function to the elements of array.</p>
  
  <p><strong>2.</strong> How can I simulate it. Can you provide the actual syntax that compiler uses to call function <code>func1</code>(how does it replace <code>ptr-&gt;func1()</code>)?</p>