C++对象模型 ch4 Function语义学
2011-09-19 21:55
309 查看
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 );
代码如下:
/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++对象模型-第四章-Function语义学(中)
- [读书笔记] 深入探索C++对象模型-第四章-Function语义学(下)
- 【深度探索c++对象模型】Function语义学之成员函数调用方式
- [读书笔记] 深入探索C++对象模型-第四章-Function语义学(中续)
- [读书笔记] 深入探索C++对象模型-第四章-Function语义学(上)
- [读书笔记] 深入探索C++对象模型-第四章-Function语义学(下续)
- 【转】图说C++对象模型:对象内存布局详解
- C++对象模型之内存布局一
- C++对象模型——临时性对象 (第六章)
- 探索c++对象模型(一)
- C++对象模型
- c++对象模型
- C++对象模型
- C++和Object Pascal对象模型
- 【深度探索C++对象模型】第二章 构造函数语意学(中)
- C++ 零散知识点速记 -- <深入探索C++对象模型> 读书笔记
- C++静态成员函数不能声明为const、volatile、virtual的原因 与 C++的对象模型
- 深度探索c++对象模型前4章读后感
- 拆一下C++ 的对象模型 (现在来看主要拆的是G++的)
- c++对象内存模型【内存布局】