动态和多态的本质是对不确定性的解释机制

意义明确的东西不需要解释;只有抽象和含糊的东西才需要解释。

 

一、解释的含义

语言的灵活性:动态、多态

是对由语言的灵活性产生的(抽象类型、动态类型)多样性和不确定性的的解释;

只有对这些多态、动态做出合理的解释才能有正确的行为;

 

这种解释更多是多态和动态函数调用的解释机制:

id objc_msgSend(id self, SEL _cmd, ...) 

 

二、解释机制

本质是编译器在类型结构的基础上添加了运行时解释机制;

编译器对由动态和多态类型的不确定行为,在语言运行机制上,在底层插入了解释机制的代码;

 

2)Compiler generates code to do dynamic binding using the vtable.

At compilation time, when compiler sees a call to a virtual method thourgh a pointer (pBase2->Hi2( )), it knows that the address of the function is only known at run time, so it will not try to find the implementation of the function. Instead, it knows that the pointer (pBase2) will be pointing to a vPtr at run time. So it generates code to go through the vPtr to find the vtable (whose composition is already know from the type of the pointer), and go to a certain entry of that vtable, fatch that function pointer, and make the call.

http://www.referencecode.org/2013/02/c-advanced-tutorial-vptr-and-vtable.html

 

三、这种解释机制是对静态编译能力不足的补充;

posted @ 2019-05-31 14:25  zzfx  阅读(269)  评论(0编辑  收藏  举报