virtual keyword internals
<p>I heard lots of times virtual function is usually implemented using a vtable. But I actually don't know actually how its implemented and how it works.</p>

<p><em>edit</em></p>

<p>I didn't actually get this code: How can it be rewritten. Can someone explain this in detail please.</p>

<p>Finally, let's see how the compiler implements a call to a virtual function. Your code might look like this:</p>

<pre><code> // Your original C++ code

 void mycode(Base* p)
 {
   p-&gt;virt3();
 }
</code></pre>

<p>The compiler has no idea whether this is going to call <code>Base::virt3()</code> or <code>Der::virt3()</code> or perhaps the <code>virt3()</code> method of another derived class that doesn't even exist yet. It only knows for sure that you are calling <code>virt3()</code> which happens to be the function in slot #3 of the v-table. It rewrites that call into something like this:</p>

<pre><code> // Pseudo-code that the compiler generates from your C++

 void mycode(Base* p)
 {
   p-&gt;__vptr[3](p);
 }
</code></pre>
