My Tech notes: Virtual Table *_vptr , VTBL, C++
Subscribe

Unix Documentation

Free Online Unix Training Materials

Lists many links to free Unix training materials.

Pointers and Arrays Materials

Pointers and Arrays materials Explained for C beginners

C FAQ and General Questions C Interview Questions

Powered By

Free XML Skins for Blogger

Powered by Blogger

Thursday, October 22, 2009

Virtual Table *_vptr , VTBL, C++

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

No comments:

Post a Comment