Pani said:
Hi
I am new to C++ ,i really dont understand why there is a need for
VTable and can some experts give an example where exactly and how a compiler
uses it
Regards
Pani
Caution, this example may imply certain views about the theory of evolution
for which the author is completely irresponsible.
#include <iostream>
using namespace std;
class Ape
{
public:
virtual void what_am_i() { cout << "i'm an ape\n"; }
};
class Human : public Ape
{
public:
virtual void what_am_i() { cout << "i'm an human\n"; }
};
int main()
{
Ape* ape1 = new Ape();
Ape* ape2 = new Human();
ape1->what_am_i();
ape2->what_am_i();
}
Would you want to be mistaken for an ape?
Although ape2 has type Ape*, it is pointing to a Human object and therefore
a Human vtable. So the compiler uses the Human vtable to call
Human::what_am_i().
john