问题描述
什么是离线虚拟方法,为什么它会影响链接时间?
What exactly is a out-of-line virtual method and why does it affect link times?
推荐答案
编译器必须为具有虚拟方法的类发出vtable.它包含这些方法的指针.如果所有虚拟方法都是内联的(在标头中定义),则编译器不知道要在其中转换vtable的转换单元(.cpp文件),因此它在所有虚拟方法中均会复制一个副本,链接程序将开始处理用它.这在目标文件中造成了额外的工作和负担.另一方面,如果离线定义了虚函数(在.cpp中),则可以将vtable在那里发出,因此将只发出一个副本.这同样适用于RTTI.
The compiler must emit a vtable for classes with virtual methods. This contains the pointers to these methods. If all the virtual methods are inline (defined in the header), then the compiler doesn't know which translation unit (.cpp file) to emit the vtable within, so it emits a copy in all of them and the linker gets to deal with it. This makes extra work and bloat in the object files. If, on the other hand, a virtual function is defined out-of-line (in a .cpp), the vtable can be emitted there and thus only one copy will be emitted. The same applies to the RTTI.
这篇关于离线虚拟方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!