473,671 Members | 2,211 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is the v-table global ?


Is the v-table, that gets generated by the compilation of a class
having virtual functions, global ? Why and how ?

Apr 18 '06 #1
5 1828
Preets wrote:
Is the v-table, that gets generated by the compilation of a class
having virtual functions, global ? Why and how ?


A vtable (if it exists) is implicit -- i.e., not explicitly accessible
to you. For certain rare circumstances, you might need to access it,
but doing so is platform-dependent. Compiler vendors are free to
organize it however they wish and may in fact implement virtual
functions by some other mechanism altogether (though I'm not aware of
any that do). Thus, any specific discussion on vtables should be taken
to the newsgroup related to your compiler/platform (consult
http://www.parashift.com/c++-faq-lit...t.html#faq-5.9 for some
possibilities). See this FAQ for some detail on how your vtable *might*
work:

http://www.parashift.com/c++-faq-lit....html#faq-20.4

Cheers! --M

Apr 18 '06 #2
Preets wrote:
Is the v-table, that gets generated by the compilation of a class
having virtual functions, global ? Why and how ?

A v-table is a behind-the-scenes implementation detail. You are
not required to know how it works, only its effects: virtual
functions and RTTI.

In practice, it's a application wide statically allocated data
(one per polymorphic class). It's not really proper to use the
word "global" because it's not "accessible " at all so scoping
really has no applicability.
Apr 18 '06 #3
Preets wrote:
Is the v-table, that gets generated by the compilation of a class
having virtual functions, global ? Why and how ?


First of all, vtables are an implementation artifact - C++ doesn't
require them. For example, virtual dispatch can be handled by the
compiler generating code that switches on the dynamic type of an object
(in some cases this is more efficient, since it allows inlining of
virtual calls).

Where the compiler uses them, vtables are generally global (though
dynamic linking can complicate this), since due to the one definition
rule, there can be only one definition of any particular type, so you
only need 1 vtable for each type that has virtual functions. Then, when
you create an object of a particular type, a pointer embedded in that
object is set to point to the relevant vtable.

As for the how, compilers tend to use heuristics to determine exactly
which translation unit (e.g. .o or .obj file) to put the vtable into.
This might be the .c file that contains the definition of the first
defined virtual function of the class, or if no such file exists (e.g.
because every function of the class is defined inline), the vtable is
emitted in every TU that needs it, and then they are coalesced at the
end by the linker.

Tom
Apr 18 '06 #4

I found the mention about global v-table at this link....
-->> http://www.parashift.com/c++-faq-lit...functions.html
under the section
-->> [20.3] What's the difference between how virtual and non-virtual
member functions are called?

To quote:
-->> if the object has one or more virtual functions, the compiler puts
a hidden pointer in the object called a "virtual-pointer" or
"v-pointer." This v-pointer points to a global table called the
"virtual-table" or "v-table."

Apr 18 '06 #5
Preets wrote:
I found the mention about global v-table at this link....
-->> http://www.parashift.com/c++-faq-lit...functions.html
under the section
-->> [20.3] What's the difference between how virtual and non-virtual
member functions are called?

To quote:
-->> if the object has one or more virtual functions, the compiler puts
a hidden pointer in the object called a "virtual-pointer" or
"v-pointer." This v-pointer points to a global table called the
"virtual-table" or "v-table."


Yes, but I would emphasize that that FAQ also says, "*Most* compilers
use some variant of the following technique...", and the following FAQ
provides the disclaimer, "Th[is] answer is entirely compiler-dependent,
so your mileage may vary, but most C++ compilers use a scheme similar
to the one presented here."

Cheers! --M

Apr 18 '06 #6

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

Similar topics

54
7414
by: sks_cpp | last post by:
What is a type_info function, more particularly, what is a type_info node? Are these related to the vtable by any means? I have seen linker errors such as: "undefined reference to SomeClass type_info node" or "undefined reference to SomeClass type_info function" What does that really mean? What is the linker looking for? Thanks.
2
2517
by: Quansheng Liang | last post by:
Hello, I struggled with the problem of "undefined reference to `vtable ...`" while migrating a project from windows to linux. After searching the google I removed all the inline functions and now the link errors decreased from over 200 to 5. A great step! But one of my class derived from wxValidator can't be linked correctly though there is no any inline function in it. The error message: -----------------------
9
3010
by: alessandro | last post by:
Compiling my current project I received the following error: undefined reference to `vtable for Tuner' The only two things I know are that the error occours when trying to invoke the constructor of a class and that concerns virtual functions. What does it mean?
7
1090
by: Karl Ebener | last post by:
Hi! I have created a program using several classes with inheritage. When I compile and link, I get the following error: :$ g++ service2.cpp Service.cpp Application.cpp Message.cpp MessageQueue.cpp MessageFragmenter.cpp SingleMessage.cpp /tmp/ccsrT4jU.o(.gnu.linkonce.t._ZN9c_ServiceD1Ev+0xb): In function `c_Service::~c_Service ()': : undefined reference to `vtable for c_Service'
4
2053
by: Clint Ruen | last post by:
Hello all, I have written out a data structure using the binary flag on an ofstream. The struct/class is something like this class SomeData { public: int data1;
4
3038
by: rahul8143 | last post by:
hello, what happens to VTABLE when base class has virtual function and derived class does not override it? Does derived class also builds VTABLE if it has no functions? eg. if code is like this(only demo code) class a{ public: virtual void fun() { cout<<"Base class";
9
13044
by: kish_nand | last post by:
Could someone please explain me the concept behind virtual functions and vtables. I am little confused about this. Please refer to the following code and tell me how many virtual tables would be created and what they would contain: class base { virtual void display() { cout<<"base display"<<endl;
17
14706
by: ypjofficial | last post by:
Hello All, I have read in many c++ literature that vtable is nothing but an array of pointer to virtual functions inside a class.And the class where the virtual function/s are declared stores the vfptr i.e the pointer to vtable internally. What confuses me is if vtable is an array of pointer to virtual functions then as per the properties of the array all the entries inside the array must be same.i.e all the pointers should be of similar...
10
3643
by: Ole Nielsby | last post by:
James Kanze <james.kanze@gmail.comwrote: COM does rely on vtable layout. COM interfaces are declared as pure virtual classes, all methods using stdcall convention, and this works because most (if not all) C++ compilers for the MSW use a very similar vtable layout. In COM, this is handled by proxies provided by system dlls,
0
8476
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8820
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8670
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6223
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5695
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1809
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.