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
7
votes
4 answers

What is the structure of virtual tables in C++?

For Example I have two "intefaces" and class type: class IPlugin { public: virtual void Load(void) = 0; virtual void Free(void) = 0; }; class IFoo { public: virtual void Foo(void) = 0; }; class Tester: public IPlugin, public…
Anton Semenov
  • 6,227
  • 5
  • 41
  • 69
7
votes
1 answer

Dynamic_cast on non polymorphic types

I can understand why dynamic_cast does work in this case : #include struct A{ virtual ~A() = default; }; struct B { virtual ~B() = default; }; struct C : A, B{}; void f(const A &a) { if(auto p = dynamic_cast
Antoine Morrier
  • 3,930
  • 16
  • 37
7
votes
2 answers

Where does C# store a structure's vtable when unmarshalling using [StructLayout(LayoutKind.Sequential)]

I have a device that transmits binary data. To interpret the data I have defined a struct that matches the data format. The struct has a StuctLayoutAttribute with LayoutKind.Sequential. This works as expected,…
Kasper van den Berg
  • 8,951
  • 4
  • 48
  • 70
7
votes
1 answer

Most efficient way to get an integer type id in a family of common base types

The problem: I have a family of objects with a common base, and I need to be able to identify the specific concrete type via an integer value. There are two obvious approaches to do that, however both come with unacceptable overheads in terms of…
dtech
  • 47,916
  • 17
  • 112
  • 190
7
votes
2 answers

How does the compiler know which entry in vtable corresponds to a virtual function?

Let's say we have more than one virtual function in the parent class and derived class. There will be a vtable created for these virtual functions in the vtable for both the parent derived class. How will the compiler know which entry in the vtable…
Ashwin
  • 499
  • 1
  • 7
  • 21
7
votes
4 answers

How to use the vtable to determine class type

I was recently on an interview for a position where C/C++ is the primary language and during one question I was told that it's possible to use the vtable to determine which class in a hierarchy a base pointer actually stores. So if, for example you…
7
votes
3 answers

Dissassembling virtual methods in multiple inheritance. How is the vtable working?

Assuming the following C++ source file: #include class BaseTest { public: int a; BaseTest(): a(2){} virtual int gB() { return a; }; }; class SubTest: public BaseTest { public: int b; SubTest(): b(4){} }; class…
J V
  • 11,402
  • 10
  • 52
  • 72
7
votes
2 answers

Size of polymorphic class derived virtually

I am having hard time to undertsand what constitutes the size of following classes? I am using MSVS 2008 (VC 9.0 compiler). I have read that if I do not declare virtual functions(in below example) then Class D will contain 2 extra pointer(1 from B…
7
votes
2 answers

Loading an EXE as a DLL, local vftable

I have an exe named test.exe which is usually used as a stand-alone application. I want to use this exe as a module (a dll) inside another application, app.exe. The code in test.exe does something really simple like: void doTest() { MyClass…
shoosh
  • 76,898
  • 55
  • 205
  • 325
7
votes
4 answers

Calling Virtual function from V-table

As all the virtual function in C++ is stored in V-table. Overiding takes place in the case of virtual function. I want to ask there is any way by which we can call the Virtual function directly from the table and able too determined what functions …
7
votes
1 answer

Why Are Vtables Not Being Implemented Correctly On Embedded Platform?

I am developing code for an embedded system (specifically, the PSoC 5, using PSoC Creator), and writing in C++. While I've overcome most hurdles with using C++ , first off compiling in C++ using the compiler flag -x c++, defining the new and delete…
gbmhunter
  • 1,747
  • 3
  • 23
  • 24
7
votes
2 answers

Dynamic Dispatch in C using virtual method table

I am hoping to find a hint (preferably by good example) for implementing dynamic dispatch in C. I am learning C and as practice, I want to translate from Java to C using dynamic dispatch virtual method table. for example I have a java code :…
Solix
  • 1,076
  • 2
  • 9
  • 13
6
votes
2 answers

Avoiding repeated C++ virtual table lookup

I have C++ program that reads a config file when the binary is executed, creates a number of child class instances based on the config file, and then periodically iterates over these instances and calls their respective virtual functions. Gprof is…
galpo
  • 183
  • 1
  • 7
6
votes
3 answers

Is it possible in IDA Pro to make a struct field offset to vtable which is defined in .data segment?

Here is what I want to achieve. I identified a class which I defined as a struct to store class data. One of the methods of the class uses class-field as if it's pointer to vtable. int __thiscall SignOn(struc_4 *this) { v1 = this; if (…
expert
  • 29,290
  • 30
  • 110
  • 214
6
votes
3 answers

Do all classes have a Vtable created for them by the compiler?

There are many resources online about VTables. They commonly have the same statement regarding them: "Whenever a class itself contains virtual functions or overrides virtual functions from a parent class the compiler builds a vtable for that class.…
xarzu
  • 8,657
  • 40
  • 108
  • 160