C++ 中的虚拟方法调用如何工作?
C++ 中的虚拟方法调用如何工作?
How does Virtual Method Invocation work in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
C++ 中的虚拟方法调用如何工作?
How does Virtual Method Invocation work in C++?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
通过虚拟表。
阅读这篇文章,http://en.wikipedia.org/wiki/Virtual_table。
我可以在这里解释,但维基百科比我做得更好。
Through virtual tables.
Read this article, http://en.wikipedia.org/wiki/Virtual_table.
I could explain it here, but the wikipedia does a better job than I could.
C++标准没有规定虚函数机制应该如何实现。
也就是说,我认为当前所有 C++ 编译器都使用虚拟表。
对于至少包含一个虚拟函数的类,执行此操作的常用方法是具有指向所谓虚拟表的隐藏指针,其中特定类的虚拟函数的地址按编译器特定的顺序输入。
然后,每个构造函数都会将此隐藏指针设置为它所属类的虚拟表。
The C++ standard doesn't specify how the virtual function mechanism should be implemented.
That said, I think all current C++ compilers use virtual tables.
The common way to do this for classes which contain at least one virtual function to have a hidden pointer to a so-called virtual table, where the addresses of the virtual functions for a specific class are entered in compiler-specific order.
Each constructor will then set this hidden pointer to the virtual table of the class it belongs to.
每个至少具有一个虚拟方法的类都有其虚拟表 - 指向该类方法的函数的指针表。
它广泛用于 COM 中。
Every class with at least one virtual method has it's virtual table - table of pointers to functions that are that class's methods.
It's extensively used in COM.
具有 VTable 和函数指针。虚函数的函数指针将在VTable中列出
MFC 使用消息映射而不是虚拟函数,这减少了大小限制。如果我们使用多个虚拟函数,VTable 最终会变得很大。
With VTables and function pointers. Virtual functions' function pointer will be listed in VTable
MFC is using Message Map instead of Virtual function, which reduces the size limitation. If we use several virtual function VTable will end up with big size.