Start New Discussion within our Software Development Community

Hi all,

I was going throughthe concept of Polymorphism.
Can anyone explain me how the vtable and vptr are created in case of virtual base class and what all values it contains.
I have idea about the vTable and Vpt in case of polymorphism that the table contains the address of all the virtual functions, but I am confused as far as virtual Base class is concerned.

Kindly explain if anyone can answer.

Thanks in advance

>Can anyone explain me how the vtable and vptr are created
>in case of virtual base class and what all values it contains.

First and foremost, each compiler will do these things in a different way. The C++ standard doesn't even mandate that virtual tables be used at all, much less how they're used.

Second, the virtual function concept really doesn't change any when you introduce virtual bases. The details get more complicated because objects need to be structured differently, and that makes the whole overriding world more difficult, but those are details you don't need to care about unless you want to write a C++ compiler or really understand your compiler.

It's actually quite safe to go only as deep as the following from an abstract machine:

  1. Objects store data members.
  2. Non-virtual inheritance merges data members of every involved class to create an object.
  3. Virtual inheritance merges only a single copy of each virtual base into an object.
  4. A non-virtual member function is basically an access-restricted non-member function.
  5. Virtual member functions use a "virtual table" which references appropriate member functions corresponding to the dynamic type of an object.
  6. Virtual member functions may incur both a size and performance cost due to the "virtual table".
This article has been dead for over six months. Start a new discussion instead.