Location of the virtual function table pointer in the object

As I understand it, the location of the function pointer table virtualin the object depends on the compiler.
Are there any pros / cons of placing this pointer at the beginning of the vs object at the end, or vice versa?

+5
source share
2 answers

The simple existence of a table of virtual functions depends on the compiler (but all compilers do), and the location does not have to ... In all compilers I know about, vptr is stored at the beginning of the object. The reason is that it provides a uniform location. Consider the class hierarchy:

struct base {
   T data;
   virtual void f();
};
struct derived : base {
   T1 data;
   virtual void g();
};

vptr , sizeof(T) base. , derived, base base, vptr sizeof(T) , - derived (sizeof(T) , sizeof(T1) ). , .

, this, vtable, vptr, , . vptr , this vptr.

+7

, .
, .
, , , , , . ++ , , . . , , ..

+4

All Articles