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 ...
随机推荐
- monkey测试入门2--测试步骤、常用参数、常规monkey命令
<凤栖梧> 柳永 伫倚危楼风细细,望极春愁,黯然生天际.草色烟光残照里,无言谁会凭栏意? 拟把疏狂图一醉,对酒当歌,强乐还无味,衣带渐宽终不悔,为伊消得人憔悴. 简要步骤:adb devi ...
- HTML中的map和area标签
1. 标签介绍: (1)map标签: 该标签是指图片的映射,也就是说一张可以点击的图片的映射: 属性介绍: <1> id: <img>中的 usemap 属性可引用 <m ...
- 【坚持】Selenium+Python学习记录 DAY11
2018/06/1-2018/06/4 参考资料: [菜鸟教程](http://www.runoob.com/python3/python3-examples.html) [Python解惑:True ...
- linux后台启动程序脚本实例
启动安装的zookeeper和kafka #!/bin/bash # start zookeeper and kafka service echo "========== Start the ...
- Cocos2dx源码赏析(2)之渲染
Cocos2dx源码赏析(2)之渲染 这篇,继续从源码的角度来跟踪下Cocos2dx引擎的渲染过程,以此来梳理下Cocos2dx引擎是如何将精灵等元素显示在屏幕上的. 从上一篇对Cocos2dx启动流 ...
- -lPods-NewsPushClientSDK is not an object file (not allowed in a library)
今天在给客户定制SDK的过程中,出现了下面的一个问题,具有代表性,现在记录下来 问题: Showing All Errors Only : /Applications/Xcode.app/Conten ...
- MySQL原生API、MySQLi面向过程、MySQLi面向对象、PDO操作MySQL
[转载]http://www.cnblogs.com/52fhy/p/5352304.html 本文将举详细例子向大家展示PHP是如何使用MySQL原生API.MySQLi面向过程.MySQLi面向对 ...
- 《JavaScript》split和join
首先了解split和join两个函数 split 根据条件截断字符串,返回数组 //str.split(option,length) 字符串转数组 //option:表示分割依据 //length:用 ...
- Task 6.2冲刺会议九 /2015-5-22
今天把之前的跳转问题加以改正并加以优化.遇到的主要问题是跳转的时后时间有点长,以为没有成功.之后查资料说是端口没有及时释放,导致了程序的滞缓.明天要继续把程序的界面进行优化.
- 关于react虚拟DOM的研究
1.传统的前端是这样的,我在学校也都是这样做的,html(jsp)主要负责提供所有的DOM节点,而javascript负责动态效果,比如按钮点击,图片轮播等,这样的话javascript如何组织结构是 ...