vtable

Alternative virtual mechanism implementations?

早过忘川 提交于 2019-11-26 14:20:44
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 compilers implement the virtual mechanism through the virtual table and virtual pointer. And yes I am aware of how this works, So my question is not about implementation detail of virtual pointers and table. My questions are: Are there any compilers which implement Virtual Mechanism in any other way other than the virtual pointer and virtual table mechanism

Print address of virtual member function

天涯浪子 提交于 2019-11-26 14:16:05
问题 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); printf("address: %p", &b->A::func); However this does not compile. Is it possible to do something like this, perhaps looking up the address in the vtable at runtime? 回答1: Currently there is no standard way of doing this in C++ although the information must

Object layout in case of virtual functions and multiple inheritance

三世轮回 提交于 2019-11-26 12:57:01
问题 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 table, insert a secret pointer to the virtual table in each object and so on). It seemed to me that there was something missing in my explanation. So here are questions (see example below) What is the exact memory layout of the object of class C.

When is a vtable created in C++?

不问归期 提交于 2019-11-26 12:14:07
问题 When exactly does the compiler create a virtual function table? 1) when the class contains at least one virtual function. OR 2) when the immediate base class contains at least one virtual function. OR 3) when any parent class at any level of the hierarchy contains at least one virtual function. A related question to this: Is it possible to give up dynamic dispatch in a C++ hierarchy? e.g. consider the following example. #include <iostream> using namespace std; class A { public: virtual void f

Virtual dispatch implementation details

时光总嘲笑我的痴心妄想 提交于 2019-11-26 11:58:44
问题 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 (correct me if I am wrong, but this isn\'t the main question). Also, I believe I know how virtual functions work , that is, I can always tell which function will be called, I just need the implementation details. Suppose someone asked me the

C++ Undefined Reference to vtable and inheritance

走远了吗. 提交于 2019-11-26 09:39:21
问题 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(); ~Child(); void doWork(); }; #endif And Child.cpp #include \"Child.h\" Child::Child(){ x = 5; } Child::~Child(){...} void Child::doWork(){...}; The compiler says that there is a undefined reference to vtable for A . I have tried lots of different things and yet none have

Virtual tables and memory layout in multiple virtual inheritance

限于喜欢 提交于 2019-11-26 08:25:09
问题 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; } }; struct B2 : virtual A { int b2; B2(int i) : A(i) { f(i); } virtual void f(int i) { cout << i+20; } }; struct C : B1, virtual B2 { int c; C() : B1(6),B2(3),A(1){} virtual void f(int i) { cout << i+30; } }; What\'s the exact memory layout of C instance? How many vptrs it

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

筅森魡賤 提交于 2019-11-26 06:16:45
问题 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++ -fdump-class-hierarchy is: Vtable for B1 B1::_ZTV2B1: 3u entries 0 (int (*)(...))0 8 (int (*)(...))(& _ZTI2B1) 16 B1::f1 Vtable for D D::_ZTV1D: 3u entries 0 (int (*)(...))0 8 (int (*)(...))(& _ZTI1D) 16 D::f1 I failed to understand what do the entries like (int ( )(...))0* correspond to

Q_OBJECT throwing &#39;undefined reference to vtable&#39; error [duplicate]

守給你的承諾、 提交于 2019-11-26 04:40:16
问题 This question already has answers here : Qt Linker Error: “undefined reference to vtable” [duplicate] (9 answers) Closed 6 years ago . 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 boundingRect() const {return QRectF();} void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {} }

Alternative virtual function calls implementations?

蓝咒 提交于 2019-11-26 03:52:12
问题 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 compilers implement the virtual mechanism through the virtual table and virtual pointer. This is not about implementation detail of virtual pointers and table. My questions are: Are there any compilers which implement dynamic dispatch of virtual functions