c++ - 虚函数指针是虚表索引吗?

标签 c++

我记得 C++ 标准说虚函数指针应该是 vtable 中的索引,并且它们在经过不同进程时仍然有效。是真的吗?有人可以指出符合这一点的严肃来源吗?

最佳答案

对于任何类型指针的表示或可转移性,在 C++ 标准中肯定没有任何保证; C++ 标准甚至没有多进程环境的概念。 (请注意,C++ 用于不存在多进程概念的嵌入式环境。)

要确定虚拟函数指针是否可以跨进程传输,请咨询您的平台 ABI。例如,Itanium ABI has :

A pointer to member function is a pair as follows:

ptr:

For a non-virtual function, this field is a simple function pointer. (Under current base Itanium psABI conventions, that is a pointer to a GP/function address pair.) For a virtual function, it is 1 plus the virtual table offset (in bytes) of the function, represented as a ptrdiff_t. The value zero represents a NULL pointer, independent of the adjustment field value below.

adj:

The required adjustment to this, represented as a ptrdiff_t.

因此在 Itanium 上,可以跨进程传输虚函数指针,只要这两个进程中的 vtable 布局相同即可。

关于c++ - 虚函数指针是虚表索引吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24679542/

相关文章:

c++ - HLSL 顶点着色器输入错误

c++ - Linux中 "write"函数的问题

c++ - 警告 C4244\'=' : conversion from 'int' to 'char' , 可能丢失数据?

c++ - C++ 中的模板和标准算法

c++ - 没有匹配函数调用 getline()?

c++ - 如何在迭代字符串时使用 "skip"空格

c++ - std::map 的 make_pair - 仅当 key 未列出时如何进行配对(否则更新 key )?

c++ - 指向成员函数的指针数组 : make it work

c++ - 计算数组中位置的巧妙方法 (C++)

c++ - Qt QComboBox 每个项目具有不同的背景颜色?