`
gaofen100
  • 浏览: 1188059 次
文章分类
社区版块
存档分类
最新评论

C++对象模型 ch4 Function语义学

 
阅读更多

1. name mangling

代码如下:


查看函数的新名,

2. Virtual table

There are three possibilities:

1. It can inherit the instance of the virtual function declared within the base class. Literally, the address of that instance is copied into the associated slot in the derived class's virtual table.

2. It can override the instance with one of its own. In this case, the address of its instance is placed within the associated slot.

3. It can introduce a new virtual function not present in the base class. In this case, the virtual table is grown by a slot and the address of the function is placed within that slot.

So if we have the expression

ptr->z();


how do we know enough at compile time to set up the virtual function call?

In general, we don't know the exact type of the object ptr addresses at each invocation of z(). We do know, however, that through ptr we can access the virtual table associated with the object's class.

Although we again, in general, don't know which instance of z() to invoke, we know that each instance's address is contained in slot 4.

This information allows the compiler to internally transform the call into

( *ptr->vptr[ 4 ] )( ptr );

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics