473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Explanation of Vptr and V-table

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

Oct 18 '06 #1
4 10421
pai wrote:
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.
Oct 18 '06 #2
"pai" <gr****@yahoo.comwrote:
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.

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
Oct 18 '06 #3
pai wrote:
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-lit....html#faq-20.4

Cheers! --M

Oct 18 '06 #4
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.

Oct 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David MacQuigg | last post by:
I am writing a chapter for teaching OOP in Python. This chapter is intended as a brief introduction to replace the more complete discussion in Learning Python, 2nd ed, pp. 295-390. I need to...
2
by: MatthewRoberts | last post by:
Howdy All, I have a Windows Service that often stops in its tracks with no exception and no explanation on our QA system. During testing on the development machine, it can handle any workload,...
2
by: Dave Taylor | last post by:
Is there a decent explanation of how menu merging with MDI forms work in VB.NET? I've read through the online help and it still seems that whenever I change menus around or whatever, it breaks...
7
by: Veeru | last post by:
Hi , What is the data type of Vptr pointer used in the virtual funtion mechanism? i suppose its void. Please correct me if i am wrong... Regards, Veeru
12
by: jacob navia | last post by:
Hi I am writing this tutorial stuff again in the holidays and I came across this problem: The "width" field in printf is a minimum width. Printf will not truncate a field. for instance:...
4
by: dismantle | last post by:
Hi all, this is my 3rd week in studying VB codes and i came across with this codes from a online tutorial about classes. Public Function MiddleInitial() As String MiddleInitial =...
3
by: Rahul | last post by:
Hi Everyone, I was wondering what is the datatype of vtpr and what is its access specifier (public or private or protected)? This is in regard to the inheritance of a class having one or more...
6
by: WolfgangS | last post by:
Ok first off, i am a total beginner at this groups stuff and i have no clue how this works. This is probabaly the wrong group for my problem but i will post it anyways. Learning by doing right? ...
4
by: slamps | last post by:
Hi, i have this code: struct MenuChan { virtual void draw()=0; }; struct MenuData_01 { int nEast; int nNorth;
16
by: DamienS | last post by:
In the interests of me saving hair, can someone please explain to me what's going on below? Why doesn't == work in comparing two int's when cast as objects? They're the same type. Note that it...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.