在VTable的上下文中,虚方法调用和直接方法调用有什么区别? [英] What is Difference between Virtual Method call and Direct Method call in context of VTable?

查看:156
本文介绍了在VTable的上下文中,虚方法调用和直接方法调用有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VTable的上下文中,虚拟方法调用和直接方法调用有什么区别?
如何解决方法参考的情况下虚拟&直接调用?

What is difference between Virtual Method call and Direct Method call in context of VTable ? How the Method reference is resolved in case of Virtual & Direct Call ?

推荐答案

理论上,没有这样的事情存在,C ++标准没有定义它

In theory, no such thing exists, the C++ standard does not define it (virtual calls are defined, but it is not specified how they must work, no such thing as a VTable exists).

在实践中,虚拟调用 使用vtable对我知道的每个编译器。这是每个类的一个地址表的内存开销,它具有虚拟成员(每个类,每个实例)和一个指向每个实例的表的指针。

调用是 - 根据架构 - 从该表加载,然后调用加载的地址,或所谓的间接调用(这是相同的,但在一个指令)。

In practice, a virtual call does use a vtable on every compiler that I know about. This is a memory overhead of one table of addresses per class that has virtual members (per class, not per instance!) and one pointer to that table per instance.
A virtual call is -- depending on architecture -- either a load from that table followed by a call to the loaded address, or a so-called indirect call (which is the same, but in one instruction).

直接调用只是一个普通的函数调用(单指令)到编译器知道的地址(存在一些共享库调用的异常,这可能使用间接甚至双间接调用好)。每当编译器100%确定对象的运行时类型,或者当你通过范围解析( operator :: )显式地告诉它时,就是所使用的。

A direct call is just a plain normal function call (single instruction) to an address that the compiler knows (there exist some exceptions with shared library calls, which may use indirect and even double-indirect calls as well). Whenever the compiler is 100% certain about an object's runtime type or when you explicitly tell it via scope resolution (operator::), that's what is used.

直接和间接调用之间的巨大差异传统上是分支预测和流水线,使得间接调用更昂贵(10-15次),但是更近的CPU对于任何一种情况(在现代CPU上有一个专用的间接调用缓存)来实现这一点。

我不会说差别是不存在或可怕的,但它肯定是没有biggie nowadays。

The big difference between direct and indirect calls has traditionally been branch prediction and pipelining, making indirect calls much more expensive (10-15 times), but more recent CPUs implement this equally well for either case (there is a dedicated indirect call cache on modern CPUs).
I won't go as far as saying the difference is non-existent or neglegible, but it sure is no biggie nowadays.

这篇关于在VTable的上下文中,虚方法调用和直接方法调用有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆