鱼C论坛

 找回密码
 立即注册
查看: 1955|回复: 0

[技术交流] 000系列2:绑定(binding)

[复制链接]
发表于 2018-2-6 14:04:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
一 &What is the definition of binding?

Call a function call to a function body is called binding.

二 &Early binding and Late binding

1. When binding is performed before the program is run (by the compiler and linker) ,it is called early binding.
2. Late binding ,which means binding occurs at runtime ,based on the type of object. When a language implements late binding ,there must be some machanism to determine the type of object at runtime and call a appropriate member function.

三 &C++ late binding implemention machanism

1. For each Class containing virtual functions (Base Class and Derived Class) ,a virtual function table (VTable ,stored in the constant area) is created in order to store the address of the virtual function in turn. For a Derived Class ,if the virtual function of its base class is not overwritten ,the virtual function address of the base class will be stored in the VTable.
2. For each object of class containing virtual functions ,a pointer (VPtr) is created to point to virtual function table of the class. So the value of objects of same class is the same(*Vptr is same ,Vptr is different). That is to say ,the virtual function is similar to class's static function at this point ,which is common to all objects (this process takes place in the process of automatically call constructed function during runtime.)
3. Assign the address/reference of the derived class object transform to a base class pointer/variety by force.
4. Then ,through a base class pointer/reference object call the virtual function ,the object of the base class will point into the virtual function table to search the address by VPtr ,which is the virtual function address of the derived class VPtr pointer before.

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-29 07:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表