简体   繁体   English

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

[英]c++: Does a vtable contains pointers to non-virtual functions?

vtable contains pointers to virtual functions of that class. vtable包含指向该类的虚函数的指针。 Does it also contains pointers to non-virtual functions as well? 它是否也包含指向非虚函数的指针?

Thx! 谢谢!

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. 如果实现将指向非虚函数的指针放入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. 存储在由vptr访问的vtable中的函数将取决于对象的动态类型,而不是依赖于它的任何静态类型的引用或指针。

No, it doesn't. 不,它没有。

As calls to non-virtual methods can be resolved during compilation (since compiler knows the addresses of non virtual functions), the compiler generates instructions to call them 'directly' (ie statically). 由于在编译期间可以解析对非虚方法的调用(因为编译器知道非虚函数的地址),编译器会生成指令以“直接”(即静态地)调用它们。

There is no reason to go through vtable indirection mechanism for methods which are known during compiling. 对于在编译期间已知的方法,没有理由通过vtable间接机制。

Whether or not a "vtable" is used by any implementation isn't defined by the standard. 标准不定义任何实现是否使用“vtable”。 Most implementations use a table of function pointers although the functions pointed to are typically not directly those being called (instead, the pointed to function may adjust the pointer before calling the actual function). 大多数实现使用函数指针表,尽管指向的函数通常不是直接调用的函数(相反,指向函数可以在调用实际函数之前调整指针)。

Whether or not non-virtual functions show up in this table is also not defined by standard. 此表中是否显示非虚拟功能也不是标准定义的。 After all, the standard doesn't even require the existence of a vtable. 毕竟,标准甚至不要求存在vtable。 Normally, non-virtual function are not in a virtual function table since any necessary pointer adjustments and call can be resolved at compile- or link-time. 通常,非虚函数不在虚函数表中,因为可以在编译或链接时解析任何必要的指针调整和调用。 I could imagine an implementation treating all functions similarly and, thus, using a pointer in the virtual function table in all cases. 我可以想象一个实现类似地处理所有函数的实现,因此,在所有情况下都使用虚函数表中的指针。 I wouldn't necessary be very popular. 我不一定非常受欢迎。 However, it might be a good way to implement C++ in an environment where it seamlessly interacts with a more flexible object system, eg, languages where individual functions can be replaced at run-time (my understanding is that something like this is possible, eg, in python). 但是,它可能是一种在与更灵活的对象系统无缝交互的环境中实现C ++的好方法,例如,在运行时可以替换单个函数的语言(我的理解是这样的事情是可能的,例如, ,在python中)。

不可以.vtable只包含指向同一类或文件中虚函数的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM