Virtual Table
C++中的虚函数(表)实现机制以及用C语言对其进行的模拟实现
C++ 多继承和虚继承的内存布局 【已翻译100%】 (虚继承参考,推荐)
VTable Notes on Multiple Inheritance in GCC C++ Compiler v4.0.1
===================================================
Why do we not have a virtual constructor in C++?
1.vptr变量是在构造函数中进行初始化的。又因为要想执行虚函数必须通过vptr变量找到虚函数表。(在构造函数初始化vptr变量之前是不会调用虚函数的)如果可以定义虚构造函数,就陷入了先有鸡还是先有蛋的问题。
2.似乎没有虚构造函数的需求
更多回答请参考 这个链接
虚析构函数
执行任何正常的析构函数:
- 执行函数体
- 销毁函数体内的自动存储期的对象
- 调用自己类的非静态数据成员的析构函数
- 调用父类的析构函数
把析构函数写成虚函数的作用,和普通虚函数的作用是一样的。
GCC编译的时候,虚表中会有两个虚析构函数。
The entries for virtual destructors are actually pairs of entries. The first destructor, called the complete object destructor, performs the destruction without calling delete() on the object. The second destructor, called the deleting destructor, calls delete() after destroying the object. Both destroy any virtual bases; a separate, non-virtual function, called the base object destructor, performs destruction of the object but not its virtual base subobjects, and does not call delete().
VS环境下的编译似乎只有一个虚析构函数。
虚继承
虚继承的实现与具体编译器相关,不同编译器实现有所不同。
class A {
public:
int a;
virtual void v();
};
class B : public virtual A {
public:
int b;
virtual void w();
};
class C : public virtual A {
public:
int c;
virtual void x();
};
class D : public B, public C {
public:
int d;
virtual void y();
};
+-----------------------+
| (vbase_offset) |
+-----------------------+
| (top_offset) |
+-----------------------+
| ptr to typeinfo for D |
+----------> +-----------------------+
d --> +----------+ | | B::w() |
| vtable |----+ +-----------------------+
+----------+ | D::y() |
| b | +-----------------------+
+----------+ | (vbase_offset) |
| vtable |---------+ +-----------------------+
+----------+ | | - (top_offset) |
| c | | +-----------------------+
+----------+ | | ptr to typeinfo for D |
| d | +-----> +-----------------------+
+----------+ | C::x() |
| vtable |----+ +-----------------------+
+----------+ | | (vbase_offset) |
| a | | +-----------------------+
+----------+ | | - (top_offset) |
| +-----------------------+
| | ptr to typeinfo for D |
+----------> +-----------------------+
| A::v() |
+-----------------------+
class A {
public:
int a;
};
class B : public virtual A {
public:
int b;
virtual void w();
};
class C : public virtual A {
public:
int c;
};
class D : public B, public C {
public:
int d;
virtual void y();
};
+-----------------------+
| (vbase_offset) |
+-----------------------+
| (top_offset) |
+-----------------------+
| ptr to typeinfo for D |
+----------> +-----------------------+
d --> +----------+ | | B::w() |
| vtable |----+ +-----------------------+
+----------+ | D::y() |
| b | +-----------------------+
+----------+ | (vbase_offset) |
| vtable |---------+ +-----------------------+
+----------+ | | - (top_offset) |
| c | | +-----------------------+
+----------+ | | ptr to typeinfo for D |
| d | +-----> +-----------------------+
+----------+
| a |
+----------+
vbase_offset指的是到基类A的偏移。
普通继承在调用virtual function时会进行查表,而存取member可以在编译期决议完成。
虚继承在调用virtual function和存取member时都会进行查表。
但无论是普通继承还是虚继承,经由对象调用(非指针、引用)都可以优化成直接存取操作,因为对象的类型不会改变。
Virtual Table的更多相关文章
- C++: Virtual Table and Shared Memory
See at: 补充栏3: C++对象和共享内存 (叙述内容和Link1的内容基本一致) <C++网络编程 卷1:运用ACE和模式消除复杂性> <C++ Network Progra ...
- virtual table for class
虚函数表 说起虚函数,相信你我都可以自然而然的想到“多态”,因为多态的实现就依赖于虚函数的继承和重写(覆盖).那么,class又或者是object是如何来管理虚函数的呢?你我又会想到虚函数表. 虚函数 ...
- (C/C++学习)4.C++类中的虚函数表Virtual Table
说明:C++的多态是通过一张虚函数表(Virtual Table)来实现的,简称为V-Table.在这个表中,主要为一个类的虚函数的地址表,这张表解决了继承.覆写的问题,保证其真实反应实际的虚函数调用 ...
- virtual table(有180个评论)
To implement virtual functions, C++ uses a special form of late binding known as the virtual table. ...
- [转] When exactly does the virtual table pointer (in C++) gets set for an object?
PS: http://stackoverflow.com/questions/7934540/when-exactly-does-the-virtual-table-pointer-in-c-gets ...
- C++对象的virtual table在内存中的布局
(1)单一继承 (2)多重继承 (3)虚拟继承 参考:<深度探索C++对象模型>
- C++: virtual inheritance and Cross Delegation
Link1: Give an example Note: I think the Storable::Write method should also be pure virtual. http:// ...
- [CareerCup] 13.3 Virtual Functions 虚函数
13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table ...
- Standard C++ Programming: Virtual Functions and Inlining
原文链接:http://www.drdobbs.com/cpp/standard-c-programming-virtual-functions/184403747 By Josée Lajoie a ...
随机推荐
- Fiddler 抓包https配置 提示:creation of the root certificate was not successful 证书安装不成功
window7 提示:creation of the root certificate was not successful 证书安装不成功 1.cmd 命令行 找到fiddler的安装目录 如 ...
- 报错:Cannot create PoolableConnectionFactory (The server time zone value 'CST' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverT
报错:Cannot create PoolableConnectionFactory (The server time zone value 'CST' is unrecognized or repr ...
- 解决网速慢时maven仓库访问慢
构建maven项目时会下载很多依赖,会从官网地址下载是个外国网站,访问速度会很慢,但可以通过修改maven的settings.xml文件换成国内的镜像地址就可以加快访问速度: 一.找到settings ...
- keyup在移动端失效解决方法
keyup在移动端失效解决方法: $("#OBJ").on("input propertychange", function(){ }); 采用 input 与 ...
- Spring Boot 学习目录
之前一直做.net 的开发,后来发现C# 在生态方面和Java还是差了好多,而且目前有很多.net 方面的技术也是借鉴了Java相关的开发,所以最近准备学习了解一下java 相关的web开发,从中学习 ...
- NO.5:自学python之路------标准库,正则表达式
引言 时间过的好快呀,终于6级也考完了,学习Python的进度也得赶赶了.好的开始这一周的内容. 正文 模块 模块的本质就是‘.py’结尾的文件,它可以用来从逻辑上组织Python代码,它可以是变量. ...
- 王者荣耀交流协会final发布-第一次scrum立会
1.例会照片 成员王超,高远博,冉华,王磊,王玉玲,任思佳,袁玥全部到齐 master:袁玥 2.时间跨度 2017年12月1日 17:00 — 17:31,总计31分钟 3.地点 一食堂二楼沙发座椅 ...
- 《JavaScript》split和join
首先了解split和join两个函数 split 根据条件截断字符串,返回数组 //str.split(option,length) 字符串转数组 //option:表示分割依据 //length:用 ...
- Task 6.1 校友聊之NABCD模型分析
我们团队开发的一款软件是“校友聊”--一个在局域网内免流量进行文字.语音.视频聊天的软件.下面将对此进行NABCD的模型分析. N(Need需求):现如今,随着网络的迅速普及,手机和电脑已经成为每个大 ...
- bata5
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...