Explanation of Vptr and V-table

P

pai

Hi,

Can any body explain me about V-table and Vptr ( virtual pointer ).
ANy link is also welcomed.
I didnt get useful explation about the topic .I mean some what detail
explation .
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.

Thank You

Regards
Pai
 
R

Ron Natalie

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.
 
D

Daniel T.

pai said:
Can any body explain me about V-table and Vptr ( virtual pointer ).
ANy link is also welcomed. I didnt get useful explation about the
topic .I mean some what detail explation . 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.

It's magic. The standard requires no v-table or virtual pointer; if one
happens to exist, it is of no concern to you or your code. But if you
really need to know, you can consult your compiler documentation about
how it handles virtual function dispatch.
 
M

mlimber

pai said:
Can any body explain me about V-table and Vptr ( virtual pointer ).
ANy link is also welcomed.
I didnt get useful explation about the topic .I mean some what detail
explation .
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.

See this FAQ:

http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.4

Cheers! --M
 
A

Amol

Hi, Pai

The best explanation about vtbl and vptr I have seen is in the Bruce
Eckel's "Thinking in C++", 2nd Ed. Vol. 2. Which is also available in
pdf and HTML formats for free on the authors personal website
www.BruceEckel.com.

Eckel devoted 4-5 pages to explain vtbl and vptr.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top