|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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. |
|