Virtual functions table offset

允我心安 提交于 2019-12-07 12:03:58

问题


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 relative 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];

回答1:


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.




回答2:


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.




回答3:


It varies from class to class.



来源:https://stackoverflow.com/questions/2125862/virtual-functions-table-offset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!