Polymorphism & Virtual Functions(Chapter 15 of Thinking in C++)

  To accomplish this, the typical compiler1 creates a single table (called the VTABLE) for each class that contains virtual functions. The compiler places the addresses of the virtual functions for that particular class in the VTABLE. In each class with virtual functions, it secretly places a pointer, called the vpointer (abbreviated as VPTR), which points to the VTABLE for that object. When you make a virtual function call through a base-class pointer (that is, when you make a polymorphic call), the compiler quietly inserts code to fetch the VPTR and look up the function address in the VTABLE, thus calling the correct function and causing late binding
to take place.
  All of this – setting up the VTABLE for each class, initializing the VPTR, inserting the code for the virtual function call – happens automatically, so you don’t have to worry about it. With virtual functions, the proper function gets called for an object

原文链接: https://www.cnblogs.com/zhtf2014/archive/2010/12/05/1897213.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    Polymorphism & Virtual Functions(Chapter 15 of Thinking in C++)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/18332

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 下午7:06
下一篇 2023年2月7日 下午7:08

相关推荐