C++ 中的虚拟方法调用如何工作?

发布于 2024-09-25 02:02:56 字数 41 浏览 4 评论 0原文

C++ 中的虚拟方法调用如何工作?

How does Virtual Method Invocation work in C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

输什么也不输骨气 2024-10-02 02:02:56

通过虚拟表。

阅读这篇文章,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.

荒芜了季节 2024-10-02 02:02:56

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.

单身情人 2024-10-02 02:02:56

每个至少具有一个虚拟方法的类都有其虚拟表 - 指向该类方法的函数的指针表。

它广泛用于 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.

单身情人 2024-10-02 02:02:56

具有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文