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

Virtual tables and memory layout in multiple virtual inheritance

Consider following hierarchy: struct A { int a; A() { f(0); } A(int i) { f(i); } virtual void f(int i) { cout << i; } }; struct B1 : virtual A { int b1; B1(int i) : A(i) { f(i); } virtual void f(int i) { cout << i+10;…
JeB
  • 11,653
  • 10
  • 58
  • 87
15
votes
3 answers

Interface vtable

Do interfaces (polymorphic class solely with pure virtual functions) have a vtable? Since interfaces do not implement a polymorphic function themself and cant be directly constructed there would be no need for the linker to place a vtable. Is that…
Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77
14
votes
3 answers

C++ Interview: vtable for a class with a pure virtual function

I was asked this interview question today!! (it was a really awkward telephonic interview..): What is the difference between the vtable for a class with virtual functions and a class with pure virtual functions? Now, I know the C++ standard…
user7
  • 2,339
  • 5
  • 25
  • 29
14
votes
3 answers

How to determine if a C++ class has a vtable?

A friend of mine sent me the following challenge earlier today: Given the following code, propose an implementation of OBJECT_HAS_VTABLE so the program prints AnObject has a vtable = 0, AnObjectWithVTable has a vtable = 1. class AnObject { …
joce
  • 9,624
  • 19
  • 56
  • 74
14
votes
4 answers

What can cause VTable pointer to be 0xdddddddd in Win32 debug build?

I am debugging a defect and have narrowed it down to the vtable pointer for an object being 0xdddddddd. This answer indicates that Win32 debug builds will generally set dead memory, or memory which has been deleted, to this special value. Note that…
LeopardSkinPillBoxHat
  • 28,915
  • 15
  • 75
  • 111
13
votes
2 answers

How to display the VTABLE of a C++ class through GCC?

I understand that a class will have a VTABLE, if it contains at-least one virtual function. I would like to see the contents of the VTABLE. Is there a way to display it ? Specifically, is there an option in gcc to display the VTABLE of a class?
nitin_cherian
  • 6,405
  • 21
  • 76
  • 127
13
votes
6 answers

Number of Virtual tables and Virtual Pointers in a C++ Program

Let say we have below program: class A { public: virtual fun(){}; }; class B:public A { public: virtual fun(){}; }; int main() { A a1; B b1; } My question is how many vtables and how many vptrs will be created, when we…
CodeCodeCode
  • 459
  • 1
  • 12
  • 21
13
votes
9 answers

Does every object of virtual class have a pointer to vtable?

Does every object of virtual class have a pointer to vtable? Or only the object of base class with virtual function has it? Where did the vtable stored? code section or data section of process?
MainID
  • 29,070
  • 19
  • 57
  • 70
13
votes
3 answers

Does making a derived C++ class "final" change the ABI?

I'm curious if marking an existing derived C++ class as final to allow for de-virtualisation optimisations will change ABI when using C++11. My expectation is that it should have no effect as I see this as primarily a hint to the compiler about how…
Dan
  • 33,953
  • 24
  • 61
  • 87
13
votes
2 answers

What is a "virtual thunk" to a virtual function that inherits from a virtual base class?

Something went wrong when I try to access the memory layout of a derived class object which inherits from a virtual base class. Programming environment: GNU/Linux 3.19.0-32-generic, x86_64 Compiler: gcc 4.8.4 //virtual base class class Base { public…
llinvokerl
  • 1,029
  • 10
  • 25
13
votes
2 answers

Vtable modifications at run time

For those compiler implementations that use vtables: are there any cases when virtual functions tables are changed at run time? Or are vtables only filled at compile time, and no actions are performed to modify them at run time?
13
votes
3 answers

Are there any costs to using a virtual function if objects are cast to their actual type?

My understanding is that virtual functions can cause performance problems because of two issues: the extra derefencing caused by the vtable and the inability of compilers to inline functions in polymorphic code. What if I downcast a variable pointer…
Kevin Salvesen
  • 293
  • 1
  • 13
13
votes
6 answers

Virtual Table layout in memory?

how are virtual tables stored in memory? their layout? e.g. class A{ public: virtual void doSomeWork(); }; class B : public A{ public: virtual void doSomeWork(); }; How will be the layout of virtual tables of class A and…
pankajt
  • 7,642
  • 12
  • 39
  • 60
12
votes
4 answers

How to use delay loading with a DLL that exports C++ classes

I have a DLL one.dll that uses a class TwoClass exported from two.dll via class __declspec(dllexport). I'd like one.dll to use /delayload for two.dll, but I get a link error: LINK : fatal error LNK1194: cannot delay-load 'two.dll' due to import of…
RichieHindle
  • 272,464
  • 47
  • 358
  • 399
12
votes
4 answers

how to determine sizeof class with virtual functions?

this is kind of homework question. For the following code, #include using namespace std; class A { public: virtual void f(){} }; class B { public: virtual void f2(){} }; class C: public A, public B { public: virtual void…
bjskishore123
  • 6,144
  • 9
  • 44
  • 66
1 2
3
41 42