开发者

Virtual functions table offset

开发者 https://www.devze.com 2022-12-17 18:18 出处:网络
I would like to ask you on what does the offset of the table of virtual functions for a class depend? I mean, from what I\'ve read it at least depends on compiler, but does it varies from class to cla

I would like to ask you on what does the offset of the table of virtual functions for a class depend? I mean, from what I've read it at least depends on compiler, but does it varies from class to class?

Edit: by offset I mean the position of the table relat开发者_开发问答ive to the address of the owner object.

Edit: example code:

void **vtable = *((void***)(((char*)object)+offset));

int **ivtable=(int **)vtable;

void* firstFunction = (void*) ivtable[0];


There is certainly a dependency on the exact class.

Remember that C++ has multiple inheritance (MI). The consequence of MI is that a single object may have multiple base subobjects. Those of course cannot be at the same address. This also means that some base subobjects don't actually start at relative offset 0.

Now, this MI introduces quite a bit of complexity with vtables: you inherit functions from multiple bases, at different offsets. For that reason it's quite common to use different vtable layouts for MI classes.

On a related note, MI also means that not every pointer to an object is actually a pointer to the start of that object. It is quite likely that a SecondBase* pointer to a Derived object is offset by sizeof(FirstBase), i.e. points somewhere in the middle of the Derived object.


As it turns out, most/all C++ compilers on the Windows platform use the same virtual function table layout. The designers of Microsoft's COM technology took advanage of this fact so that C++ implementations of COM interfaces can be compiled on all the standard Windows C++ compilers.

I don't have the exact order memorised, but it basically comes down to depth-first left recursion on the object graph, so that the first declared virtual method on the left-most (1st-declared if multiple inheritance is used), deepest derived class is the first virtual method in the table. I don't know if "virtual" inheritance (using the virtual keyword in the declaration of the base class) is standardized, though.


It varies from class to class.

0

精彩评论

暂无评论...
验证码 换一张
取 消