pai said:
Hi,
Can any body explain me about V-table and Vptr ( virtual pointer ).
They are internal guts to the way compilers MIGHT implmeent virtual
functions and RTTI (dynamic_cast, typeinfo). By and large you need
be unconcerned with them. They vary from compiler to compiler and
even depending on the compiler switches.
As what happens when i add virtual keyword to a function and so is
done by the compiler at that time and also if I inherit the base class
which has a virtual function will it create one more v-Table and Vptr.
Again this is implementation specific, but the truth is that as
soon as you have a polymorphic class (any virtual functions in it),
there is a slight overhead created (which is why the language
distinguishes between polymorphic class and not). Similarly
a virtual function may have small performance overheads on it's
invocation when compared to a non-virtual function.
If you inherit from a polymorphic class, even if the new class
doesn't declare any virtual functions, it is also polymorphic.
The above is the official language guidance.
Now, that being said, most compilers do use a vtable to implement
these concepts. A vtable is a list of pointers to virtual
function implementations for each virtual function defined (or
inheritted within) a function as well as a pointer (or the
actual data) for the typeinfo values. Only one vtable is
created per class defined (not per object). Each object
gets a pointer to the vtable so each object of a polymorphic
class gets larger by one pointer (the vptr) that points to
it's vtable.
That's fairly reliable. The layout of the vtables and how
the pointers got to get bashed around plus the vagaries of
how you have to bash pointers to members to get everything
to work are highly implementation specific.