其类型在编译时已知的虚拟方法
如果我这样做:
Dog dog; //class with virtual methods
Cat cat; //class from same base as Dog
dog.eat(); //call virtual method
cat.eat(); //call virtual method
那么 eat() 将是正常的方法调用,并且不需要 v 表 - 正确吗? 我可以假设它的运行方式与非虚拟方法相同吗?
(是的,我知道编译器如何处理虚函数并不在标准中 - 我想知道大多数编译器是做什么的)
If I do something like:
Dog dog; //class with virtual methods
Cat cat; //class from same base as Dog
dog.eat(); //call virtual method
cat.eat(); //call virtual method
Then the eat()s will be normal method calls and will not require the v-table - correct?
I can assume that it would run identical to a non-virtual method?
(and yes, I know that how compilers handle virtual functions is not in the standard - I want to know what most compilers do)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您使用
object.member
时 - 您不会取消引用指针,因此对virtual
方法没有影响。仅当您有一个可以多态的指针并且使用动态调度时,Virtual
才会生效。例如:
编辑按评论更正
When you're using
object.member
- you're not dereferencing a pointer, so there's no effect to thevirtual
methods.Virtual
comes into effect only when you have a pointer which can be polymorphic and dynamic dispatch is used.For example:
edit corrected per comments
如果分析显示大多数时间使用某种类型,则 Visual Studio 能够将虚拟调用优化为直接调用。
请参阅 MSDN 上的配置文件引导优化。
常规的“去虚拟化”(即其他答案所提到的)可以在不分析应用程序的情况下完成,并且非常常见。
GCC 自动启用此优化,但具体标志为 -fdevvirtualize:
来自 GCC 优化选项。
Visual Studio is able to optimize a virtual call into a direct call if profiling shows that a certain type is used most of the time.
See Profile Guided Optimization on MSDN.
Regular "devirtualization" (i.e. what the other answers allude to) can be done without profiling the app, and is pretty common.
GCC enables this optimization automatically, but the specific flag is -fdevirtualize:
from GCC Optimize Options.
如果静态类型在编译时已知,则无需使用 vtable,因为无论如何要调用的函数都是已知的。
大多数编译器都会看到这一点。
If the static type is known at compile time there is no need to use the vtable, as the function to call in known anyway.
Most compilers will see this.
我想说的是,大多数编译器都会进行这种优化。如果您已经进行了分析并且它确实很重要,那么唯一确定的方法就是检查生成的反汇编。
I would say that yes, most compilers would do this optimization. If you've profiled and it actually matters, the only way to know for sure is to check the disassembly generated.