Questions tagged [vtable]

A virtual table, or vtable, is a technique used to implement polymorphic functions with dynamic dispatch

In C++ (and other languages) a vtable contains pointers to a polymorphic type's virtual functions, allowing the language runtime to do dynamic dispatch by looking up the relevant entry in an object's vtable. The lookup is generally implemented by accessing the table at a fixed offset determined at compile time.

A object of a polymorphic type contains a pointer to its class' vtable and the vtable contains a pointer to the final overrider of each virtual function (i.e. the implementation of the function in the most-derived type that overrides the function.)

617 questions
33
votes
3 answers

Why can't virtual functions use return type deduction?

n3797 says: § 7.1.6.4/14: A function declared with a return type that uses a placeholder type shall not be virtual (10.3). Therefore the following program is ill-formed: struct s { virtual auto foo() { } }; All I can find for the…
user3920237
31
votes
3 answers

Virtual method tables

When discussing sealed classes, the term "virtual function table" is mentioned quite frequently. What exactly is this? I read about a method table a while ago (I don't remember the purpose of the purpose of this either) and a google/search on here…
GurdeepS
  • 65,107
  • 109
  • 251
  • 387
27
votes
5 answers

Why do we need a virtual table?

I was looking for some information about virtual tables, but I can't find anything that is easy to understand. Can somebody give me good examples with explanations?
lego69
  • 767
  • 1
  • 12
  • 20
27
votes
6 answers

When is a vtable created in C++?

When exactly does the compiler create a virtual function table? 1) when the class contains at least one virtual function. OR 2) when the immediate base class contains at least one virtual function. OR 3) when any parent class at any level of the…
user236215
  • 7,278
  • 23
  • 59
  • 87
26
votes
4 answers

Dynamic method dispatching in C

I know it sounds silly and I know that C is not an Object Oriented Language. But is there any way that dynamic method dispatching can be achieved in C? I thought about function pointers but don't get the entire idea. How could I implement this?
Dineshkumar
  • 4,165
  • 5
  • 29
  • 43
24
votes
9 answers

How do upcasting and vtables work together to ensure correct dynamic binding?

So, vtable is a table maintained by the compiler which contains function pointers that point to the virtual functions in that class. and Assigning a derived class's object to an ancestor class's object is called up-casting. Up-casting is handling a…
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
23
votes
6 answers

Performance hit of vtable lookup in C++

I'm evaluating to rewrite a piece of real-time software from C/assembly language to C++/assembly language (for reasons not relevant to the question parts of the code are absolutely necessary to do in assembly). An interrupt comes with a 3 kHz…
user2711077
  • 231
  • 1
  • 2
  • 6
22
votes
4 answers

Virtual Table C++

I read a lot of people writing "a virtual table exists for a class that has a virtual function declared in it". My question is, does a vtable exists only for a class that has a virtual function or does it also exist for classes derived from that…
Frank Q.
  • 6,001
  • 11
  • 47
  • 62
20
votes
5 answers

understanding vptr in multiple inheritance?

I am trying to make sense of the statement in book effective c++. Following is the inheritance diagram for multiple inheritance. Now the book says separate memory in each class is required for vptr. Also it makes following statement An oddity in…
Xinus
  • 29,617
  • 32
  • 119
  • 165
20
votes
6 answers

How do virtual functions work in C# and Java?

How do the virtual functions work in C# and Java? Does it use same vtable and vpointer concept similar to C++ or is it something totally different?
Naveen
  • 74,600
  • 47
  • 176
  • 233
19
votes
5 answers

Qt: Signals and slots Error: undefined reference to `vtable for

Following example from this link: http://developer.kde.org/documentation/books/kde-2.0-development/ch03lev1sec3.html #include #include #include using namespace std; class MyWindow : public QWidget { Q_OBJECT …
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
18
votes
2 answers

C++: Class specialization a valid transformation for a conforming compiler?

Hopefully this isn't too specialized of a question for StackOverflow: if it is and could be migrated elsewhere let me know... Many moons ago, I wrote a undergraduate thesis proposing various devirtualization techniques for C++ and related languages,…
17
votes
2 answers

How do objects work in x86 at the assembly level?

I'm trying to understand how objects work at the assembly level. How exactly are objects stored in memory, and how do member-functions access them? (editor's note: the original version was way too broad, and had some confusion over how assembly and…
sgmm
  • 580
  • 2
  • 7
  • 13
16
votes
2 answers

Can C++ compilers optimize repeated virtual function calls on the same pointer?

Suppose I have the following code void f(PolymorphicType *p) { for (int i = 0; i < 1000; ++i) { p->virtualMethod(something); } } Will the compiler's generated code dereference p's vtable entry for virtualMethod 1 or 1000 times? …
japreiss
  • 11,111
  • 2
  • 40
  • 77
16
votes
8 answers

Why vptr is not static?

Every class which contains one or more virtual function has a Vtable associated with it. A void pointer called vptr points to that vtable. Every object of that class contains that vptr which points to the same Vtable. Then why isn't vptr static ?…
Samarsh
  • 565
  • 5
  • 18
1
2
3
41 42