Does C++ virtual function call on derived object go through vtable?

后端 未结 6 535
情书的邮戳
情书的邮戳 2020-12-11 17:13

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?<

6条回答
  •  眼角桃花
    2020-12-11 17:57

    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:

    int main() {}
    

提交回复
热议问题