top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is virtual function in C++?

+3 votes
329 views

Also provide some details on how virtual table get created and how that works?

posted Nov 25, 2013 by Prakash Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes

By default, C++ matches a function call with the correct function definition at compile time. This is called static binding. You can specify that the compiler match a function call with the correct function definition at run time; this is called dynamic binding. You declare a function with the keyword virtual if you want the compiler to use dynamic binding for that specific function.

Or in simple terms we can say that a virtual function a function whose behavior can be overridden within an inheriting class by a function with the same signature.

Example

class Shape
{
    public:
       virtual void draw() = 0;
       virtual ~Shape() {}
};

class Rectange: public Shape
{
    public:
        void draw() { // draw rectangle here } 
};


class Circle: public Shape
{
    public:
       void draw() { // draw circle here }
};

Now you can have vector of different shapes:

vector<Shape*> shapes;
shapes.push_back(new Rectangle());
shapes.push_back(new Circle());

And you can draw all shapes like this:

for(vector<Shape*>::iterator i = shapes.begin(); i != shapes.end(); i++)
{
      (*i)->draw();
}

Virtual Table
To implement virtual functions, C++ uses a special form of late binding known as the virtual table. The virtual table is a lookup table of functions used to resolve function calls in a dynamic/late binding manner. The virtual table sometimes goes by other names, such as “vtable”, “virtual function table”, “virtual method table”, or “dispatch table”.

The virtual table is actually quite simple, though it’s a little complex to describe in words. First, every class that uses virtual functions (or is derived from a class that uses virtual functions) is given it’s own virtual table. This table is simply a static array that the compiler sets up at compile time. A virtual table contains one entry for each virtual function that can be called by objects of the class. Each entry in this table is simply a function pointer that points to the most-derived function accessible by that class.

Second, the compiler also adds a hidden pointer to the base class, which we will call *__vptr. *__vptr is set (automatically) when a class instance is created so that it points to the virtual table for that class. Unlike the *this pointer, which is actually a function parameter used by the compiler to resolve self-references, *__vptr is a real pointer. Consequently, it makes each class object allocated bigger by the size of one pointer. It also means that *__vptr is inherited by derived classes, which is important.

answer Nov 25, 2013 by Sanketi Garg
Similar Questions
+2 votes

I would like to know, if there is any difference (in allocated memory space) between defining a function inline in the class declaration (with the inline word), and defining a function inline explicit after the class declaration.

+2 votes

When a function is declared as virtual and when a class ? Please explain with examples.

0 votes

Explain about pure virtual function

0 votes

What is a pure virtual destructor in C++? What is its need, advantage and disadvantage, please explain with example?

...