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
12
votes
2 answers

Deciphering vtable dumps

I am "playing" with virtual inheritance in C++, and I want to know how a class object is laid out. I have those three classes: class A { private: int a; public: A() {this->a = 47;} virtual void setInt(int x) {this->a = x;} virtual…
pvinis
  • 4,059
  • 5
  • 39
  • 59
12
votes
2 answers

vtable in polymorphic class of C++ using gdb

How to display vtable using a pointer to base class object having virtual functions?
user303205
12
votes
4 answers

c++: Does a vtable contains pointers to non-virtual functions?

vtable contains pointers to virtual functions of that class. Does it also contains pointers to non-virtual functions as well? Thx!
rahul
  • 6,447
  • 3
  • 31
  • 42
11
votes
3 answers

Why does virtual inheritance need a vtable even if no virtual functions are involved?

I read this question: C++ Virtual class inheritance object size issue, and was wondering why virtual inheritance results in an additional vtable pointer in the class. I found an article here: https://en.wikipedia.org/wiki/Virtual_inheritance which…
Klaus
  • 24,205
  • 7
  • 58
  • 113
11
votes
1 answer

Why do vtables have sizeof(void*) * 2 bytes of 0x00 padding?

I imagine this is implementation-specific, but for armv7, arm64, and x86_64 builds using libstdc++ and libc++ (gcc or clang), it seems that vtables always have 8 bytes (16 on 64-bit) of padding at the beginning, and fetching a vtable usually looks…
Ryan Terry
  • 181
  • 8
11
votes
2 answers

C++ v-table: Part of the language or compiler dependent?

Is the v-table (virtual method table) a part of the C++ specification, or is it up to the compiler to solve the virtual method lookups? In case it's part of the spec: Why? I'd guess that it's compiler dependent, but someone said to me that it's part…
aioobe
  • 413,195
  • 112
  • 811
  • 826
11
votes
1 answer

C++ virtual functions: Can the linker remove entries in the virtual function table which aren't called?

This question is a kind of followup to eliminate unused virtual functions, which does not go deep enough for my interest. The problem: When defining classes that have virtual functions, the compiler allocates storage for the virtual function table,…
sh-
  • 941
  • 6
  • 13
10
votes
1 answer

Alternatives to vtable

Vtables are ubiquitous in most OO implementations, but do they have alternatives? The wiki page for vtables has a short blurb, but not really to much info (and stubbed links). Do you know of some language implementation which does not use…
Thunker
  • 101
  • 3
10
votes
1 answer

Are vtables generated for all types that implement a trait?

If I have a trait Foo, and some implementors Bar, Baz. impl Foo for Bar { } impl Foo for Baz { } But say I only use one of them ever as a trait object, let bar = Bar {..}; let foo: &dyn Foo = &bar; Then will my binary still have vtable for…
zombiesauce
  • 1,009
  • 1
  • 7
  • 22
10
votes
1 answer

What's the advantage of the "hand-rolled" vtable approach?

Recently, I've come across a couple of type-erasure implementations that use a "hand-rolled" vtable - Adobe ASL's any_regular_t is one example, although I've seen it used in Boost ASIO, too (for the completion routine queue). Basically, the parent…
gmbeard
  • 674
  • 6
  • 19
10
votes
3 answers

Virtual tables and virtual pointers for multiple virtual inheritance and type casting

I am little confused about vptr and representation of objects in the memory, and hope you can help me understand the matter better. Consider B inherits from A and both define virtual functions f(). From what I learned the representation of an…
Artium
  • 5,147
  • 8
  • 39
  • 60
10
votes
4 answers

C++ Inheritance/VTable questions

Update: Replaced the destructor example with a straight up method call example. Hi, If I have the following code: class a { public: virtual void func0(); // a has a VTable now void func1(); }; class b : public a { public: void func0() {…
jameszhao00
  • 7,213
  • 15
  • 62
  • 112
10
votes
4 answers

Object layout in case of virtual functions and multiple inheritance

I was recently asked in an interview about object layout with virtual functions and multiple inheritance involved. I explained it in context of how it is implemented without multiple inheritance involved (i.e. how the compiler generated the virtual…
Ankur
  • 11,239
  • 22
  • 63
  • 66
9
votes
2 answers

How do I suppress C++ vtable generation for pure virtual classes using G++?

Supressing C++ vtable generation can be done in MSVC using the __declspec(novtable) attribute. However, it seems that there is no equivalent attribute for the GNU C++ compiler. The fact is that leaving the vtables for pure virtual classes…
fincs
  • 207
  • 3
  • 8
9
votes
5 answers

When exactly does the virtual table pointer (in C++) gets set for an object?

I know that for any class that has a virtual function or a class that is derived from a class that has a virtual function, the compiler does two things. First, it creates a virtual table for that class and secondly, it puts a virtual pointer (vptr)…
Abhineet Mishra
  • 113
  • 1
  • 6