Does an abstract classes have a VTABLE?

前端 未结 3 1137
春和景丽
春和景丽 2020-12-10 13:58

Do we have virtual table for an abstract class?

3条回答
  •  离开以前
    2020-12-10 14:30

    First of all, usage of vtables is implementation defined and not mandated by the standard.

    For implementations that use vtable, the answer is: Yes, usually. You might think that vtable isn't required for abstract classes because the derived class will have its own vtable, but it is needed during construction: While the base class is being constructed, it sets the vtable pointer to its own vtable. Later when the derived class constructor is entered, it will use its own vtable instead.

    That said, in some cases this isn't needed and the vtable can be optimized away. For example, MS Visual C++ provides the __declspec(novtable) flag to disable vtable generation on pure interface classes.

提交回复
热议问题