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
9
votes
1 answer

Why do trait object vtables contain size and alignment?

Rust's trait objects are fat pointers that contain 2 regular pointers: to data and to a vtable. The vtable is a structure containing a destructor function pointer, all trait method pointers and finally the size and alignment of the data. What are…
CodeSandwich
  • 1,601
  • 13
  • 23
9
votes
4 answers

How to get rid of virtual table? Sealed class

As far as I know making class sealed gets rid of look up in VTable or am I wrong? If I make a class sealed does this mean that all virtual methods in class hierarchy are also marked sealed? For example: public class A { protected virtual void…
9
votes
3 answers

Inheritance Costs in C++

Taking the following snippet as an example: struct Foo { typedef int type; }; class Bar : private Foo { }; class Baz { }; As you can see, no virtual functions exist in this relationship. Since this is the case, are the the following assumptions…
chrosph
  • 115
  • 2
9
votes
4 answers

COM method offsets in Delphi

In Delphi, how do I find out the the address of a COM method? I can hardcode the offsets //0 is the offset of the QueryInterface method p := TPonterArray(pointer(SomeInterface)^)[0]; but I would prefer to use symbolic names. The folllowing…
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
9
votes
3 answers

Workaround for lack of return type covariance when overriding virtual methods

is there any way to 'hack' or 'coerce' covariant overrides in to C#? For example: public class Alpha { public virtual Alpha DoSomething() { return AlphaFactory.GetAlphaFromSomewhere(); } } public class Beta : Alpha { public…
Xenoprimate
  • 7,691
  • 15
  • 58
  • 95
9
votes
1 answer

How is the deletion of a pointer detected using dynamic cast

As shown here, one can use dynamic_cast to detect a deleted pointer: #include using namespace std; class A { public: A() {} virtual ~A() {} }; class B : public A { public: B() {} }; int main() { B* pB = new B; cout <<…
azerty
  • 698
  • 7
  • 28
9
votes
1 answer

Explanation of virtual table

Possible Duplicate: Understanding the vtable entries Using g++ version 4.6.3, 64-bit machine . I know compiler is free to implement virtual functions any way it wants. I want to know what happened here. My class: #include class test { …
Anon
  • 2,608
  • 6
  • 26
  • 38
8
votes
3 answers

Position-independent code and vtable

How are virtual functions implemented in position-independent code? I know that if my class has virtual functions, the compiler usually generates a vtable for it that contains addresses of all virtual functions, and stores a pointer to the vtable in…
anatolyg
  • 26,506
  • 9
  • 60
  • 134
8
votes
5 answers

virtual table and _vptr storage scheme

Can someone explains how this virtual table for the different class is stored in memory? When we call a function using pointer how do they make a call to function using address location? Can we get these virtual table memory allocation size using a…
Vineet Jain
  • 1,515
  • 4
  • 21
  • 31
8
votes
6 answers

Low level details of inheritance and polymorphism

This question is one of the big doubts that looms around my head and is also hard to describe it in terms of words . Some times it seems obvious and sometimes a tough one to crack.So the question goes like this:: class Base{ public: int…
Arunmu
  • 6,837
  • 1
  • 24
  • 46
8
votes
1 answer

vtable: Underlying algorithm

My understanding of vtables is that, if I have a class Cat with a virtual function speak() with subclasses Lion and HouseCat, there is a vtable which maps speak() to the correct implementation for each Subclass. So a call cat.speak() Compiles…
Alex
  • 871
  • 7
  • 23
8
votes
4 answers

How are C++ vtable methods ordered *In Practice*

In theory, C++ does not have a binary interface, and the order of methods in the vtable is undefined. Change anything about a class's definition and you need to recompile every class that depends upon it, in every dll etc. But what I would like to…
Tuntable
  • 3,276
  • 1
  • 21
  • 26
8
votes
4 answers

Where is pure virtual function located in C++?

Which virtual table will be pure virtual function located? In the base class or derived class? For example, what does the virtual table look like in each class? class Base { virtual void f() =0; virtual void g(); } class Derived: public…
skydoor
  • 25,218
  • 52
  • 147
  • 201
8
votes
5 answers

Under what circumstances can a vtable pointer be null (or 0x1)?

I am currently debugging a crashlog. The crash occurs because the vtable pointer of a (c++-) object is 0x1, while the rest of the object seems to be ok as far as I can tell from the crashlog. The program crashes when it tries to call a virtual…
Tobias
  • 6,388
  • 4
  • 39
  • 64
8
votes
3 answers

Undefined reference to `typeinfo for class' and undefined reference to `vtable for class'

I'm dealing with inheritance in C++. I wanted to write a program for addition and subtraction of two arrays. Heres my code: #include #include #include using namespace std; class root { protected : int…
Brian Brown
  • 3,873
  • 16
  • 48
  • 79