When is a vtable created in C++?

后端 未结 6 441
面向向阳花
面向向阳花 2020-12-01 03:16

When exactly does the compiler create a virtual function table?

1) when the class contains at least one virtual function.

OR

2) when the immediate

6条回答
  •  粉色の甜心
    2020-12-01 03:45

    The behavior is defined in chapter 10.3, paragraph 2 of the C++ language specification:

    If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name and same parameter list as Base::vf is declared, then Derived::vf is also virtual ( whether or not it is so declared ) and it overrides Base::vf.

    A italicized the relevant phrase. Thus, if your compiler creates v-tables in the usual sense then all classes will have a v-table since all their f() methods are virtual.

提交回复
热议问题