我记得 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 aptrdiff_t
. The value zero represents a NULL pointer, independent of the adjustment field value below.adj:
The required adjustment to this, represented as aptrdiff_t
.
因此在 Itanium 上,可以跨进程传输虚函数指针,只要这两个进程中的 vtable 布局相同即可。
关于c++ - 虚函数指针是虚表索引吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24679542/