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.)

554 questions
122
votes
12 answers

How are virtual functions and vtable implemented?

We all know what virtual functions are in C++, but how are they implemented at a deep level? Can the vtable be modified or even directly accessed at runtime? Does the vtable exist for all classes, or only those that have at least one virtual…
Brian R. Bondy
  • 314,085
  • 114
  • 576
  • 619
69
votes
9 answers

Qt Linker Error: "undefined reference to vtable"

This is my header: #ifndef BARELYSOCKET_H #define BARELYSOCKET_H #include //! The First Draw of the BarelySocket! class BarelySocket: public QObject { Q_OBJECT public: BarelySocket(); public slots: void sendMessage(Message…
Thomas
  • 1,425
  • 3
  • 13
  • 24
68
votes
5 answers

Q_OBJECT throwing 'undefined reference to vtable' error

I'm using Qt Creator 2.0.1 with Qt 4.7.0 (32 bit) on Windows 7 Ultimate 32 bit. Consider the following code, which is a minimum to produce the error: class T : public QObject, public QGraphicsItem { Q_OBJECT public: T() {} QRectF …
Donotalo
  • 11,954
  • 23
  • 75
  • 111
51
votes
5 answers

Can't downcast because class is not polymorphic?

Is it possible to have inheritance with no virtual methods? The compiler is saying that the following code is not polymorphic. Example: class A { public: int a; int getA(){return a;}; } class B : public A { public: int b; int…
wfbarksdale
  • 6,962
  • 11
  • 60
  • 87
51
votes
3 answers

What is the first (int (*)(...))0 vtable entry in the output of g++ -fdump-class-hierarchy?

For this code: class B1{ public: virtual void f1() {} }; class D : public B1 { public: void f1() {} }; int main () { B1 *b1 = new B1(); D *d = new D(); return 0; } After compilation, the vtable I get with g++…
Aquarius_Girl
  • 18,558
  • 57
  • 191
  • 353
47
votes
11 answers

Alternative virtual function calls implementations?

C++ supports dynamic binding through virtual mechanism. But as I understand the virtual mechanism is an implementation detail of the compiler and the standard just specifies the behaviors of what should happen under specific scenarios. Most…
Alok Save
  • 190,255
  • 43
  • 403
  • 518
47
votes
5 answers

Undefined symbols "vtable for ..." and "typeinfo for..."?

Nearly the final step but still some strange erros.... bash-3.2$ make g++ -Wall -c -g Myworld.cc g++ -Wall -g solvePlanningProblem.o Position.o AStarNode.o PRM.o PRMNode.o World.o SingleCircleWorld.o Myworld.o RECTANGLE.o CIRCLE.o -o…
Lisa
  • 481
  • 3
  • 7
  • 10
41
votes
1 answer

How are java interfaces implemented internally? (vtables?)

C++ has multiple inheritance. The implementation of multiple inheritance at the assembly level can be quite complicated, but there are good descriptions online on how this is normally done (vtables, pointer fixups, thunks, etc). Java doesn't have…
JanKanis
  • 5,166
  • 3
  • 31
  • 38
37
votes
7 answers

Undefined reference to 'vtable for xxx'

takeaway.o: In function `takeaway': project:145: undefined reference to `vtable for takeaway' project:145: undefined reference to `vtable for takeaway' takeaway.o: In function `~takeaway': project:151: undefined reference to `vtable for…
TacticalMin
  • 781
  • 1
  • 7
  • 18
35
votes
5 answers

Print address of virtual member function

I am trying to print the address of a virtual member function. If I know which class implements the function I can write: print("address: %p", &A::func); But I want to do something like this: A *b = new B(); printf("address: %p", &b->func);…
hidayat
  • 8,463
  • 13
  • 44
  • 63
34
votes
3 answers

Virtual dispatch implementation details

First of all, I want to make myself clear that I do understand that there is no notion of vtables and vptrs in the C++ standard. However I think that virtually all implementations implement the virtual dispatch mechanism in pretty much the same way…
Armen Tsirunyan
  • 120,726
  • 52
  • 304
  • 418
32
votes
2 answers

What is the VTT for a class?

Recently ran across a C++ linker error that was new to me. libfoo.so: undefined reference to `VTT for Foo' libfoo.so: undefined reference to `vtable for Foo' I recognized the error and fixed my problem, but I still have a nagging question: what…
leedm777
  • 21,698
  • 10
  • 53
  • 84
31
votes
3 answers

C++ Undefined Reference to vtable and inheritance

File A.h #ifndef A_H_ #define A_H_ class A { public: virtual ~A(); virtual void doWork(); }; #endif File Child.h #ifndef CHILD_H_ #define CHILD_H_ #include "A.h" class Child: public A { private: int x,y; public: Child(); …
rjmarques
  • 339
  • 1
  • 3
  • 10
29
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
29
votes
6 answers

Where in memory is vtable stored?

Where in memory is vtable stored?
Sandeep
  • 2,737
  • 5
  • 21
  • 17
1
2 3
36 37