VTBL or VPTR nothing but virtual table and virtual pointer in C++.
It gets created when we create function with virtual keyword. As soon as we create the virtual function in a class A table gets created behind the code.
BaseClass
{
virtual void function1(){...}
}
ChildClass : public BaseClass
{
virtual void function1(){...}// method overridden.
}
When we override method that is create another version of function in derived class ChildClass in above case then the *_vptrvirtual void function1(){...}// method overridden. gets created for BaseClass.
So VTBL Of Base Class has entry for function1 and Child class will be pointing to that function. *_vptr is nothing but the pointer to function.
Note: ones the virtual keyword is introduced all the upcoming functions are considered as virtual.
ChildClass: public BaseClass
{
virtual void function1(){...}// method overridden.
void function2(){}
}
In above case function2 also considered as virtual.
BaseClass will not be pointing to any of Child Class functions and ChildClass will be pointing to all the virtual functions whose entry is in VTBL of baseclass as soon as the object of the base class instantiated or the method is overridden.
Tags : pointer, virtual, C++, function pointer,VTBL, VPTR, Function overriding , polymorphism
It gets created when we create function with virtual keyword. As soon as we create the virtual function in a class A table gets created behind the code.
BaseClass
{
virtual void function1(){...}
}
ChildClass : public BaseClass
{
virtual void function1(){...}// method overridden.
}
When we override method that is create another version of function in derived class ChildClass in above case then the *_vptrvirtual void function1(){...}// method overridden. gets created for BaseClass.
So VTBL Of Base Class has entry for function1 and Child class will be pointing to that function. *_vptr is nothing but the pointer to function.
Note: ones the virtual keyword is introduced all the upcoming functions are considered as virtual.
ChildClass: public BaseClass
{
virtual void function1(){...}// method overridden.
void function2(){}
}
In above case function2 also considered as virtual.
BaseClass will not be pointing to any of Child Class functions and ChildClass will be pointing to all the virtual functions whose entry is in VTBL of baseclass as soon as the object of the base class instantiated or the method is overridden.
Tags : pointer, virtual, C++, function pointer,VTBL, VPTR, Function overriding , polymorphism
Comments