虚拟功能表偏移 [英] Virtual functions table offset

查看:111
本文介绍了虚拟功能表偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问你一个类的虚拟函数表的偏移量取决于什么?我的意思是,从我读过的内容来看,至少取决于编译器,但是它会因类而异吗?



编辑:偏移量我的意思是表
$ b

编辑:示例代码:

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

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

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


解决方案

确切的类肯定存在依赖关系。

请记住,C ++有多重继承(MI)。 MI的后果是单个对象可能有多个基本子对象。那些当然不能在同一个地址。这也意味着一些基本子对象实际上并不以相对偏移0开始。

现在,这个MI引入了很多vtables的复杂性:你从多个基地,在不同的抵消。因此,MI类使用不同的vtable布局是相当普遍的。

在一个相关说明中,MI也意味着并不是每个指向对象的指针实际上都是指向 该对象的开始。指向 Derived 对象的 SecondBase * 指针很可能会被 sizeof( FirstBase),即指向 Derived 对象中间的某处。


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

解决方案

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.

这篇关于虚拟功能表偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆