且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

c++:vtable 是否包含指向非虚函数的指针?

更新时间:2022-05-23 01:52:59

这是一个实现细节,但不是.如果实现将指向非虚函数的指针放入 vtable 中,则不能使用这些指针进行函数调用,因为这通常会导致调用不正确的非虚函数.

It's an implementation detail, but no. If an implementation put pointers to non-virtual functions into a vtable it couldn't use these pointers for making function calls because it would often cause incorrect non-virtual functions to be called.

当调用非虚拟函数时,实现必须使用调用该函数的对象的静态类型来确定要调用的正确函数.存储在 vptr 访问的 vtable 中的函数将依赖于对象的动态类型,而不是访问它的引用或指针的任何静态类型.

When a non-virtual function is called the implementation must use the static type of the object on which the function is being called to determine the correct function to call. A function stored in a vtable accessed by a vptr will be dependent on the dynamic type of the object, not any static type of a reference or pointer through which it is being accessed.