If the base class defines a virtual function, and the inheritance class does not reload this virtual function, in the virtual function table of the base class and the inheritance class, their virtual function addresses are the same. If you reload this virtual function, the addresses of the virtual functions in the two virtual functions are different. Therefore, we made a validation of this statement on the visual c 6.0 platform. See:
1. When the inherited class does not overload the virtual function defined by the base class
# Include <iostream. h>
Class
{
Public:
Virtual void f ()
{
Cout <"class A" <endl;
};
};
Class B: public {};
Int main ()
{
A * pa = new;
Int * paadd = (int *) (* (int *) pa );
Int * paaddr = (int *) (* (int *) paadd );
Cout <paaddr <endl;
A * pb = new B;
Int * pbadd = (int *) (* (int *) pb );
Int * pbaddr = (int *) (* (int *) pbadd );
Cout <pbaddr <endl;
Delete pa;
Delete pb;
Return 0
}
The output is indeed the same, all of which are 0x00401028 (note: the addresses on different machines may be different)
2. When the inheritance class reloads the virtual functions defined by the base class
# Include <iostream. h>
Class
{
Public:
Virtual void f ()
{
Cout <"class A" <endl;
};
};
Class B: public
{
Public:
Virtual void f ()
{
Cout <"class B" <endl;
};
};
Int main ()
{
A * pa = new;
Cout <sizeof (* pa) <endl;
Int * paadd = (int *) (* (int *) pa );
Int * paaddr = (int *) (* (int *) paadd );
Cout <paaddr <endl;
A * pb = new B;
Int * pbadd = (int *) (* (int *) pb );
Int * pbaddr = (int *) (* (int *) pbadd );
Cout <pbaddr <endl;
Delete pa;
Delete pb;
Return 0;
}
In this case, the virtual function address of the base class is: 0x00401028, and the virtual function address of the inherited class is: 0x00401032.