Tuesday, April 3, 2012
what is virtual table or Vtable in C++?
In C++ for implementing virtual functions , compiler uses Virtual table or simply Vtable. This is also called virtual function table or virtual method table. Lets see what is it and how it works.
As you know virtual functions are used for run time binding or function overriding. If a class contains a virtual function, you will get one virtual table for that class. This applies for derived classes also. So for each class which contains atleast one virtual function, we will be having corresponding Vtable. Compiler create this Vtable at the time of compilation. Vtable is a static array which contians one entry for each virtual fucntion that can be called by object. Each entry in the Vtable is a function pointer to the "most derived" function.
The compiler will also add hidden pointer called *_vptr to the Vtable for the base class. *_vptr will be inherited to the derived class Vtable also. *_vptr pointer adds to the Vtable automatically when instance is creatd. See the below sample code snippet and image for more clarity.
In the above code there is base claass vtBase with two virtual functions display() and displayMore(). There are two derived classes in which derivedD is override the function display() with new definition and derivedDD is overrdes the definition for displayMore(). The corresponding Vtable is given below.
what happens when object creation:
Output:
this is derived display function
In the above main fucntion, in line no. 5, by seeing this, compiler finds display() is virtual function, and then it gets vPtr->display() value as derivedD virtual table from there it looks for the display() function pointer. all this happens in three steps
Using this Vtable process the compiler resolves to the proper virtual function even if you use pointers and references. You can also access the Vtable pointer by using _vptr. see the sample code snippet below.
Output:
this is derived display function
_vptr is 0x400cb0
this is dispaly function
_vptr is 0x400d30
As you know virtual functions are used for run time binding or function overriding. If a class contains a virtual function, you will get one virtual table for that class. This applies for derived classes also. So for each class which contains atleast one virtual function, we will be having corresponding Vtable. Compiler create this Vtable at the time of compilation. Vtable is a static array which contians one entry for each virtual fucntion that can be called by object. Each entry in the Vtable is a function pointer to the "most derived" function.
The compiler will also add hidden pointer called *_vptr to the Vtable for the base class. *_vptr will be inherited to the derived class Vtable also. *_vptr pointer adds to the Vtable automatically when instance is creatd. See the below sample code snippet and image for more clarity.
class vtBase { public: virtual void display() { cout<<"this is dispaly function"<<endl; } virtual void displayMore() { cout<<"this is dispalyMore function"<<endl; } }; class derivedD:public vtBase { public: void display() // display function overriding { cout<<"this is derived display function"<<endl; } }; class derivedDD:public vtBase { public: void displayMore() //displayMore function overriding { cout<<"this is derived displayMore function"<<endl; } };
In the above code there is base claass vtBase with two virtual functions display() and displayMore(). There are two derived classes in which derivedD is override the function display() with new definition and derivedDD is overrdes the definition for displayMore(). The corresponding Vtable is given below.
Virtual Table
In the above Vtable, base class *_vptr is pointing to the base class vtBase table and its member functions are function pointers to the base class. And derived class *_vptr are pointing to the derived calls Vtable pointer. And in derived class derivedD , you will have two member functions derived from base class, in that one member function display() is overrides with new definition. So its "pointer pointing to the derived member" function and displayMore function "pointer points to the base class" pointer. Similarly for the derived classe derivedDD, but here displayMore() is overrides with new definition. So this function pointer points to derived class and display() function pointer points to the base class.what happens when object creation:
main() { derivedD D; // creating derived object vtBase *bPtr=&D; // assining to base pointer bPtr->display(); }
Output:
this is derived display function
In the above main fucntion, in line no. 5, by seeing this, compiler finds display() is virtual function, and then it gets vPtr->display() value as derivedD virtual table from there it looks for the display() function pointer. all this happens in three steps
- finding function as virtual
- calling _vptr for getting Vtable address
- finding the pointer for the function
Using this Vtable process the compiler resolves to the proper virtual function even if you use pointers and references. You can also access the Vtable pointer by using _vptr. see the sample code snippet below.
main() { derivedD D; // creating derived object vtBase b; // creating base object vtBase *bPtr=&D; // assining to base pointer bPtr->display(); cout<<"_vptr is "<<bPtr->_vptr<<endl; //accessing the _vptr bPtr=&b; // assining to base pointer bPtr->display(); cout<<"_vptr is "<<bPtr->_vptr<<endl;//accessing the _vptr }
Output:
this is derived display function
_vptr is 0x400cb0
this is dispaly function
_vptr is 0x400d30
Subscribe to:
Post Comments (Atom)
Popular Posts
-
A universally unique identifier ( UUID ) is an identifier standard used in software construction, standardized by the Open...
-
Recently I started working on Japser Studio professional for my new project Cloud to generate the reports. I was very new to all cloud ...
-
Below is C program for AVL Tree implementation. #include<stdio.h> #include<malloc.h> typedef struct bst { int info; int hei...
-
strcmp is another string library function which is used to compare two strings and returns zero if both strings are same , returns +ve valu...
-
One of the complex operation on binary search tree is deleting a node. Insertion is easy by calling recursive insertion. But deletion wont...
-
We have recently faced one tricky issue in AWS cloud while loading S3 file into Redshift using python. It took almost whole day to inde...
-
Object slicing: when a derived class object is assigned to a base class object. only base class data will be copied from derived class and...
-
We have faced lot of weird issues while loading S3 bucke t files into redshift. I will try to explain all issues what we faced. Before go...
-
Below code is to find the cube root of a given integer number with out using pow math library function. Its very simple and brute force...
-
Recently we faced one issue in reading messages from SQS in AWS cloud where we are processing same message multiple times. This issue we...
2 comments:
Very clear and detailed explanation. thanks A lot...
Thanks for postingg this
Post a Comment