vptr

When does the vptr (pointing to vtable) get initialized for a polymorphic class?

风流意气都作罢 提交于 2020-01-31 02:21:30
问题 This is not about "When VTABLE is created?". Rather, when the VPTR should be initialized? Is it at the beginning/end of the constructor or before/after the constructor? A::A () : i(0), j(0) -->> here ? { -->> here ? //... -->> here ? } 回答1: The machinery for virtual calls (usually a v-table, but doesn't need to be) is set up during the ctor-initializer , after construction of base subobjects and before construction of members. Section [class.base.init] decrees: Member functions (including

Type trait to identify primary base class

烂漫一生 提交于 2019-12-30 08:23:10
问题 If I have a class Base, with at least one virtual function, and a class Derived which inherits singly from this then (uintptr_t)derived - (uintptr_t)static_cast<Base*>(derived) is guaranteed (by the Itanium ABI) to be zero, even though Derived is not standard layout. However in the general case this is not necessarily true (eg. multiple inheritance). Is it possible to write a trait which can be used to detect if one class is the primary base class of another? Useful sections from the Itanium

Virtual inheritance and empty vtable in base class

梦想的初衷 提交于 2019-12-21 06:09:30
问题 There is this code: #include <iostream> class Base { int x; }; class Derived : virtual public Base { int y; }; int main() { std::cout << sizeof(Derived) << std::endl; // prints 12 return 0; } I have read that when some class is virtually inherited then there is created empty vtable for class Derived, so memory layout is as follows: Derived::ptr to empty vtable Derived::y Base::x and it is 12 bytes. The question is - what is purpose of this empty vtable if there are not any virtual methods and

understanding vptr in multiple inheritance?

大兔子大兔子 提交于 2019-12-20 08:38:56
问题 I am trying to make sense of the statement in book effective c++. Following is the inheritance diagram for multiple inheritance. Now the book says separate memory in each class is required for vptr. Also it makes following statement An oddity in the above diagram is that there are only three vptrs even though four classes are involved. Implementations are free to generate four vptrs if they like, but three suffice (it turns out that B and D can share a vptr), and most implementations take

Alternative schemes for implementing vptr?

匆匆过客 提交于 2019-12-19 17:40:07
问题 This question is not about the C++ language itself(ie not about the Standard) but about how to call a compiler to implement alternative schemes for virtual function. The general scheme for implementing virtual functions is using a pointer to a table of pointers. class Base { private: int m; public: virtual metha(); }; equivalently in say C would be something like struct Base { void (**vtable)(); int m; } the first member is usually a pointer to a list of virtual functions, etc. (a piece of

Determine the size of object without its virtual table pointers

主宰稳场 提交于 2019-12-13 15:19:26
问题 Is there a generic way (not platform dependent) to get at compile time the size of a class object in the memory, without counting the vtable pointers? 回答1: As you are asking for a portable way: class MyClass { private: struct S { DataMemberType1 dataMember1; ... DataMemberTypeN dataMemberN; } m; public: static const size_t MemberSize = sizeof(S); }; 回答2: Use sizeof on this class , it doesn't include size of the vtable just the pointer. 来源: https://stackoverflow.com/questions/28433973

Virtual class inheritance object size issue

扶醉桌前 提交于 2019-12-10 19:05:41
问题 Here, in this code, the size of ob1 is 16 which is fine(because of the virtual pointer) but I can't understand why the size of ob2 is 24. #include <iostream> using namespace std; class A { int x; }; class B { int y, z; }; class C : virtual public A { int a; }; class D : virtual public B { int b; }; int main() { C ob1; D ob2; cout << sizeof(ob1) << sizeof(ob2) << "\n"; } I expect the size of ob2 as 20, but the output is 24 回答1: One possible layout for objects of type D is: +----------+ | y |

Why does virtual inheritance need a vtable even if no virtual functions are involved?

我们两清 提交于 2019-12-10 12:48:01
问题 I read this question: C++ Virtual class inheritance object size issue, and was wondering why virtual inheritance results in an additional vtable pointer in the class. I found an article here: https://en.wikipedia.org/wiki/Virtual_inheritance which tells us: However this offset can in the general case only be known at runtime,... I don't get what is runtime-related here. The complete class inheritance hierarchy is already known at compile time. I understand virtual functions and the use of a

Why is vptr stored as the first entry in the memory of a class with virtual functions?

雨燕双飞 提交于 2019-12-07 08:10:54
问题 For some compilers, if a class has virtual functions then its vptr can be accessed with the address of the first byte of its object. For instance, class Base{ public: virtual void f(){cout<<"f()"<<endl;}; virtual void g(){cout<<"g()"<<endl;}; virtual void h(){cout<<"h()"<<endl;}; }; int main() { Base b; cout<<"Address of vtbl:"<<(int *)(&b)<<endl; return 0; } I know that it is dependent on different compiler behaviors. Since there is the case where vptr is stored as the very first entry, what

Why does my C++ object loses its VPTr

不羁岁月 提交于 2019-12-07 02:15:47
问题 While debugging one of the program's core dump I came across the scenario where its contained object which is polymorphic loses its VPTr and I can see its pointing to NULL. What could be the scenario when an object loses its VPTr. Thanks in advance, Brijesh 回答1: The memory has been trashed, i.e. something overwrote the memory. You destroyed it by calling delete or by invoking the destructor directly. This typically does not NULL out the vptr, it will just end up having it point to the vtable