Memory layout of a class under multiple or virtual inheritance and the vtable(s)?

家住魔仙堡 提交于 2019-12-02 18:23:17

C++ implementations generally use vtables to implement virtual functions. A vtable is a table of pointers to functions. Each object of a class with virtual functions has a hidden pointer to the vtable containing the addresses of all the virtual functions of the class.

When invoking a virtual function, the code calculates the offset of the function pointer in the vtable, and calls the function which address is stored there.

When a derived class of the base class overides a virtuall function, the virtual table of that class just points to the overidden function instead of the original one.

This excellent article explains in details how it work, both for single and multiple inheritance.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!