vptr

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

北城余情 提交于 2019-12-05 14:28:05
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 is the advantage of doing this? Does that help improve performance or simply because it's easier to

Why does my C++ object loses its VPTr

大憨熊 提交于 2019-12-05 08:09:52
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 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 of the base class, but that depends on your implementation. Most likely, case 1. If you have a debugger that

Virtual inheritance and empty vtable in base class

雨燕双飞 提交于 2019-12-03 20:29:58
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 how is it used? Derived needs some way to know where the Base subobject is. With virtual inheritance,

Virtual tables and virtual pointers for multiple virtual inheritance and type casting

雨燕双飞 提交于 2019-12-03 10:29:30
问题 I am little confused about vptr and representation of objects in the memory, and hope you can help me understand the matter better. Consider B inherits from A and both define virtual functions f() . From what I learned the representation of an object of class B in the memory looks like this: [ vptr | A | B ] and the vtbl that vptr points to contains B::f() . I also understood that casting the object from B to A does nothing except ignoring the B part at the end of the object. Is it true?

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

拈花ヽ惹草 提交于 2019-12-03 00:29:41
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 ? } 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 virtual member functions, 10.3) can be called for an object under construction. Similarly, an object under

Virtual tables and virtual pointers for multiple virtual inheritance and type casting

你。 提交于 2019-12-02 23:50:38
I am little confused about vptr and representation of objects in the memory, and hope you can help me understand the matter better. Consider B inherits from A and both define virtual functions f() . From what I learned the representation of an object of class B in the memory looks like this: [ vptr | A | B ] and the vtbl that vptr points to contains B::f() . I also understood that casting the object from B to A does nothing except ignoring the B part at the end of the object. Is it true? Doesn't this behavior is wrong? We want that object of type A to execute A::f() method and not B::f() . Are

understanding vptr in multiple inheritance?

时光毁灭记忆、已成空白 提交于 2019-12-02 16:22:11
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 advantage of this opportunity to reduce the compiler-generated overhead. I could not see any reason why

Type trait to identify primary base class

旧城冷巷雨未停 提交于 2019-12-01 04:13:20
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 ABI: http://refspecs.linux-foundation.org/cxxabi-1.83.html Primary base class For a dynamic class, the

Why vptr is not static?

时光毁灭记忆、已成空白 提交于 2019-11-30 05:02:21
Every class which contains one or more virtual function has a Vtable associated with it. A void pointer called vptr points to that vtable. Every object of that class contains that vptr which points to the same Vtable. Then why isn't vptr static ? Instead of associating the vptr with the object, why not associate it with the class ? The runtime class of the object is a property of the object itself. In effect, vptr represents the runtime class, and therefore can't be static . What it points to, however, can be shared by all instances of the same runtime class. Your diagram is wrong. There is

How many vptr will a object of class(uses single/multiple inheritance) have?

China☆狼群 提交于 2019-11-29 10:35:42
问题 How many vptrs are usually needed for a object whose clas( child ) has single inheritance with a base class which multiple inherits base1 and base2. What is the strategy for identifying how many vptrs a object has provided it has couple of single inheritance and multiple inheritance. Though standard doesn't specify about vptrs but I just want to know how an implementation does virtual function implementation. 回答1: Why do you care? The simple answer is enough , but I guess you want something