C++对象模型 ch4 Function语义学
来源:互联网 发布:淘宝 淘气值 编辑:程序博客网 时间:2024/06/18 16:39
1. name mangling
代码如下:
/home/a/j/nomad2:cat Point.CPP #include <iostream>using namespace std;class Point{ private: int pinx; int piny; public: void foo() { cout << (pinx + piny) << endl; } int foo(int arg) { cout << (pinx + piny) << endl; } static void func() { cout << "hi dude" << endl; }};int main(){ Point p; p.foo(); p.foo(2); ((Point*)0)->func();}
查看函数的新名,
/home/a/j/nomad2:nm a.out |grep foo[79] | 68320| 56|FUNC |GLOB |0 |9 |_ZN5Point3fooEi[82] | 68256| 48|FUNC |GLOB |0 |9 |_ZN5Point3fooEv/home/a/j/nomad2:nm a.out |grep func[69] | 68392| 44|FUNC |GLOB |0 |9 |_ZN5Point4funcEv
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 );
- C++对象模型 ch4 Function语义学
- 《深度探索c++对象模型》 学习笔记 - 4 Function语义学
- [读书笔记] 深入探索C++对象模型-第四章-Function语义学(上)
- [读书笔记] 深入探索C++对象模型-第四章-Function语义学(中)
- [读书笔记] 深入探索C++对象模型-第四章-Function语义学(中续)
- [读书笔记] 深入探索C++对象模型-第四章-Function语义学(下)
- [读书笔记] 深入探索C++对象模型-第四章-Function语义学(下续)
- 深度探索C++对象模型---Function语义学之Member的各种调用方式
- C++对象模型 ch3 Data语义学
- C++对象模型 ch6 Running语义学
- 深度探索C++对象模型----Data语义学
- 深度搜索C++对象模型 - 数据语义学
- 深度搜索C++对象模型 - 函数语义学
- 深度搜索C++对象模型 - 构造语义学
- 深度探索C++对象模型-Data语义学
- C++对象模型--4.Data语义学
- 深度探索C++对象模型第二章 构造函数语义学
- 深度探索C++对象模型第六章 执行其语义学
- 彻底卸载MySQL的方法
- 进制转换问题
- UML-1
- 实训——UML
- 【mac os x安装方案】新手安装指南:一步一步在Windows安装苹果雪豹系统【远景】
- C++对象模型 ch4 Function语义学
- ibatis + Spring 多表查询(3)
- 设计模式概要
- javaIO复习第二阶段
- 有关IsIconic 确定给定窗口是否是最小化(图标化)的窗口
- 高分求解一个需求JQUEYR SELECT TEXT
- 免费的魔力有多大?!免费商业模式完全指南
- oracle 11 修改主机hostname 后listener 无法启动
- 编译器后端寄存器分配算法SSA(静态单一赋值法)