First of all, This question sounds to be repetitive but I explored this over google, but didn't get satisfactory answer as a layman
Question: Let's say we have 3 virtual function in the parent class and derived class as shown in below example
class Base1 {
public :
virtual void Print1() {
cout << "Print1 Base1" << endl;
}
virtual void Print2() {
cout << "Print1 Base1" << endl;
}
virtual void Print3() {
cout << "Print1 Base1" << endl;
}
};
class Derived : public Base1 {
public:
void Print1() {
cout << "Print1 Derived1" << endl;
}
void Print2() {
cout << "Print1 Derived2" << endl;
}
void Print3() {
cout << "Print1 Derived3" << endl;
}
};
int main()
{
Derived d;
Base1 *bptr = &d;
bptr->Print2(); // -> Here Derived::Print2() shall be called
return 0;
}
As shown in above example , Derived::Print2() shall be called in main()
Here , Base class and Derived class vTable shall consist of 3 entries for 3 virtual functions
[0] -> Address of Print1()
[1] -> Address of Print2()
[2] -> Address of Print3()
I wanted to know , At runtime how compiler searches Print2() in Derived vTable ?