Hello All
Recently I go through the concept of virtual function in C++.
I have confusion with how many vitual pointer and virtual table for a single class are created?
And if the class is inheriing some other then what will be the situation?

I have confusion with how many vitual pointer and virtual table for a single class are created?

Each class has a virtual table and each object has a pointer to that table.

And if the class is inheriing some other then what will be the situation?

If there's no inheritance then the virtual table mechanism is pointless, ne? So wouldn't it stand to reason that in light of inheritance, the situation is as described above?

Consider this simple example:

class Foo {
  public:
    virtual void f();     //one virtual function
    virtual void g() = 0; //one pure-virtual function
};

class Bar : public Foo {
  public:
    virtual void f(); //overridden 
    virtual void g(); //overridden
    virtual void h(); //one additional virtual function
};

Now, class Foo will have a virtual table that looks like this (lets assume all the function pointers are stored as void*, to make it simple):

void* Foo_vtable[] = {&Foo::f, //pointer to member function f() in Foo.
                      NULL};   //null-pointer since g() is pure-virtual.

And the virtual table for Bar will look like this:

void* Bar_vtable[] = {&Bar::f, //pointer to member function f() in Bar
                      &Bar::g, //pointer to member function g() in Bar
                      &Bar::h}; //pointer to member function h() in Bar

Notice how the additional entry (function h()) is simply appended to the list, this way, the start of the table looks exactly like the vtable of Foo and can be interpreted as such.

Also, since the virtual table is the same for all objects of one class, there is only one virtual table per class (well, multiple inheritance can complicate things a bit more, but that's the gist of it).

When writing a derived class, the compiler will generate the correct virtual table for it. If there are functions which are pure-virtual, they will have a null-entry and also make the class "abstract" which means an object cannot be created from that class directly since it would mean a null function pointer in the virtual table which will cause a crash if called (attempted to be called). Moreover, if the derived class leaves a virtual function unchanged (not redefined), then the compiler will use the entry of the base class for that slot in the virtual table.

Each object that is of a class that has one or more virtual functions will have a pointer to its virtual table. That pointer is implicitly initialized by the most derived class upon construction so that it points to the correct virtual table.

When calling a virtual function, the mechanism used (called "dynamic dispatch") consists of a simple look-up of the pointer in the virtual table and a call to whichever function it points to. That is, unless the actual class of an object can be deduced by the compiler, in which case the dynamic dispatching is not necessary and the virtual function is called like any regular member function (and can be inlined in some cases).

thank you mike_2000_17

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.