C++ 吗?派生对象上的虚函数调用要通过vtable吗?
在下面的代码中,它通过指向派生对象的指针调用虚拟函数 foo。这个调用会通过 vtable 还是会直接调用 B::foo
?
如果它通过 vtable,那么让它直接调用 B::foo
的 C++ 惯用方法是什么?我知道在这种情况下我总是指向 B
。
Class A
{
public:
virtual void foo() {}
};
class B : public A
{
public:
virtual void foo() {}
};
int main()
{
B* b = new B();
b->foo();
}
In the following code, it calls a virtual function foo via a pointer to a derived object. Will this call go through the vtable or will it call B::foo
directly?
If it goes via a vtable, what would be a C++ idiomatic way of making it call B::foo
directly? I know that in this case I am always pointing to a B
.
Class A
{
public:
virtual void foo() {}
};
class B : public A
{
public:
virtual void foo() {}
};
int main()
{
B* b = new B();
b->foo();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果启用了优化,大多数编译器都足够聪明,可以消除这种情况下的间接调用。但这只是因为您刚刚创建了对象并且编译器知道动态类型;可能存在您知道动态类型而编译器不知道的情况。
Most compilers will be smart enough to eliminate the indirect call in that scenario, if you have optimization enabled. But only because you just created the object and the compiler knows the dynamic type; there may be situations when you know the dynamic type and the compiler doesn't.
像往常一样,这个问题的答案是“如果这对您很重要,请查看发出的代码”。这是 g++ 在未选择优化的情况下产生的结果:
它使用 vtable。由如下代码生成的直接调用:
如下所示:
As usual, the answer to this question is "if it is important to you, take a look at the emitted code". This is what g++ produces with no optimisations selected:
which is using the vtable. A direct call, produced by code like:
looks like this:
是的,它将使用 vtable(只有非虚拟方法绕过 vtable)。要直接在
b
上调用B::foo()
,请调用b->B::foo()
。Yes, it will use the vtable (only non-virtual methods bypass the vtable). To call
B::foo()
onb
directly, callb->B::foo()
.这是使用 -O3 从 g++ (4.5) 编译的代码。
它所做的唯一优化是它知道要使用哪个 vtable(在 b 对象上)。否则“call *_ZTV1B+16(%rip)”将是“movq (%rax), %rax; call *(%rax)”。
所以g++在优化虚函数调用方面实际上相当糟糕。
This is the compiled code from g++ (4.5) with -O3
The only optimization it did was that it knew which vtable to use (on the b object). Otherwise "call *_ZTV1B+16(%rip)" would have been "movq (%rax), %rax; call *(%rax)".
So g++ is actually quite bad at optimizing virtual function calls.
编译器可以优化虚拟调度并直接调用虚拟函数或内联它(如果可以证明它是相同的行为)。在提供的示例中,编译器将轻松丢弃每一行代码,因此您将得到的是:
Compiler can optimize away virtual dispatch and call virtual function directly or inline it if it can prove it's the same behavior. In the provided example, compiler will easily throw away every line of code, so all you'll get is this:
我对代码做了一些修改,以便自己尝试一下,对我来说,它看起来像是删除了 vtable,但我在 asm 方面还不够专业,无法判断。我确信一些评论员会纠正我的观点:)
然后我将这段代码转换为如下所示的程序集:
在我看来,有趣的部分就像“opt”版本正在删除 vtable。看起来它正在创建 vtable,但没有使用它。
在 opt asm:
和相同的 base.asm 版本中:
在第 93 行,我们在注释中看到:
_vptr.A
我'我很确定这意味着它正在执行 vtable 查找,但是,在实际的主函数中,它似乎能够预测答案,甚至不调用该 useIt 代码:我认为这只是说,我们知道我们要返回2,让我们把它放在eax中。 (我重新运行该程序,要求它返回 200,并且该行已按我的预期更新)。
额外的一点
所以我把程序变得更复杂了一点:
在这个版本中,useIt代码肯定使用了优化汇编中的vtable:
这一次,main函数内联了
useIt<的副本/code>,但实际上执行了 vtable 查找。
c++11 和“final”关键字怎么样?
所以我将一行更改为:
并将编译器行更改为:
认为告诉编译器它是最终重写,将允许它跳过也许是虚函数表。
事实证明它仍然使用vtable。
所以我的理论答案是:
I changed the code up a bit to give it a go myself, and to me it looks like it's dropping the vtable, but I'm not expert enough in asm to tell. I'm sure some commentators will set me right though :)
I then converted this code to assembly like this:
And the interesting bits look to me like the 'opt' version is dropping the vtable. It looks like it's creating the vtable but not using it..
In the opt asm:
and the base.asm version of the same:
On line 93 we see in the comments:
_vptr.A
which I'm pretty sure means it's doing a vtable lookup, however, in the actual main function, it seems to be able to predict the answer and doesn't even call that useIt code:which I think is just saying, we know we're gonna return 2, lets just put it in eax. (I re ran the program asking it to return 200, and that line got updated as I would expect).
extra bit
So I complicated the program up a bit more:
In this version, the useIt code definitely uses the vtable in the optimized assembly:
This time, the main function inlines a copy of
useIt
, but does actually do the vtable lookup.What about c++11 and the 'final' keyword?
So I changed one line to:
and the compiler line to:
Thinking that telling the compiler that it is a final override, would allow it to skip the vtable maybe.
Turns out it still uses the vtable.
So my theoretical answer would be: