Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
Hi,


I Understand that, like how to create a virtual function and what is use of it ,but internally how vtable gets created and how function call get executed during runtime.


Regards,
Ranjith
Posted
Updated 23-Dec-13 1:45am
v2
Comments
Pablo Aliskevicius 23-Dec-13 7:49am    
Are you writing a compiler?
If not, google VTABLE and read the first 5 results. At least 2 should match your curiosity.
If you're writing a compiler, you'll need a big pile of books.

 
Share this answer
 
Comments
CPallini 23-Dec-13 7:53am    
5, of course.
Richard MacCutchan 23-Dec-13 8:51am    
Google of course. :)
 
Share this answer
 
See for instance this Wikipedia page: "Virtual method table"[^].
 
Share this answer
 
Quote:
how to create a virtual function and what is use of it


One usage is to create abstract classes for example:
C++
class Employee
{
...
virtual void getSalary() = 0; // by doing this you're creating an abstract class which means you can only inherit from it and can't create an object of it.
...
};


Another usage, suppose you have two classes Parent and Child. The Child class inherits from Parent class and Parent class has its own display function which will display for example "I'm parent", and the Child class has an overridden function display which will display for example "I'm child".
And you've this function:
C++
void mFunction(Parent &p)
{
    p.display();
}


If you don't use virtual keyword for display function (virtual void display();) in parent's class then no matter what you pass Parent class object or Child class object it will display "I'm parent".

If you use virtual keyword
then it will display "I'm parent" if it is a Parent class object
or it will display "I'm child" if it is a child class object.

There might be other usages that I don't know about and I hope someone will help you with that.
Good luck.

Regards,
Mohammed Abdulgaffar.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900