C++对象模型——吴泰

C/C++杂记

C++中的虚函数(表)实现机制以及用C语言对其进行的模拟实现

C++ 多继承和虚继承的内存布局 【已翻译100%】 (虚继承参考,推荐)

图说C++对象模型:对象内存布局详解

VTable Notes on Multiple Inheritance in GCC C++ Compiler v4.0.1

Itanium C++ ABI

What is the VTT for a class?

===================================================

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的更多相关文章

  1. C++: Virtual Table and Shared Memory

    See at: 补充栏3: C++对象和共享内存 (叙述内容和Link1的内容基本一致) <C++网络编程 卷1:运用ACE和模式消除复杂性> <C++ Network Progra ...

  2. virtual table for class

    虚函数表 说起虚函数,相信你我都可以自然而然的想到“多态”,因为多态的实现就依赖于虚函数的继承和重写(覆盖).那么,class又或者是object是如何来管理虚函数的呢?你我又会想到虚函数表. 虚函数 ...

  3. (C/C++学习)4.C++类中的虚函数表Virtual Table

    说明:C++的多态是通过一张虚函数表(Virtual Table)来实现的,简称为V-Table.在这个表中,主要为一个类的虚函数的地址表,这张表解决了继承.覆写的问题,保证其真实反应实际的虚函数调用 ...

  4. virtual table(有180个评论)

    To implement virtual functions, C++ uses a special form of late binding known as the virtual table. ...

  5. [转] 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 ...

  6. C++对象的virtual table在内存中的布局

    (1)单一继承 (2)多重继承 (3)虚拟继承 参考:<深度探索C++对象模型>

  7. C++: virtual inheritance and Cross Delegation

    Link1: Give an example Note: I think the Storable::Write method should also be pure virtual. http:// ...

  8. [CareerCup] 13.3 Virtual Functions 虚函数

    13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table ...

  9. Standard C++ Programming: Virtual Functions and Inlining

    原文链接:http://www.drdobbs.com/cpp/standard-c-programming-virtual-functions/184403747 By Josée Lajoie a ...

随机推荐

  1. PHPCMS V9 二次开发常用代码集

    0:调用最新文章,带所在版块 {pc:get sql="SELECT a.title, a.catid, b.catid, b.catname, a.url as turl ,b.url a ...

  2. jquery 3.0 新版本

    https://code.jquery.com/

  3. Nginx笔记(一):安装

    Nginx在安装前需要先安装其所依赖的类库,所以需先行安装好之后再进行Nginx安装. Nginx依赖以下模块: l  gzip模块需要 zlib 库 l  rewrite模块需要 pcre 库 l  ...

  4. nodejs 中 module.exports 和 exports 的区别

    1. module应该是require方法中,上下文中的对象 2. exports对象应该是上下文中引用module.exports的新对象 3. exports.a = xxx 会将修改更新到mod ...

  5. tty命令详解

    基础命令学习目录首页 原文链接:http://blog.chinaunix.net/uid-9525959-id-2001836.html [功能]  打印连接到标准输入的终端的文件名. [描述]  ...

  6. iframe子页面position的fixed

    前言: 首先说一说我昨天天的苦逼经历.中午吃饭时一同事跟我说,他做的项目嵌套iframe后,子页面的position设置fixed失效了. 经过反复询问,得知他用了两层iframe,再加上最外的父页面 ...

  7. Navicat将oracle中数据复制到mysql

    1,首先两个数据库都要处于连接状态 2,工具 -- 数据传输 3,选择来源数据库以及要传输的表和目标数据库 4,点击开始 PS:遇到一个问题:[Err] [Dtf] 1426 - Too-big pr ...

  8. Python中,os.listdir遍历纯数字文件乱序如何解决

    Python中,os.listdir遍历纯数字文件乱序如何解决 日常跑深度学习视觉相关代码时,常常需要对数据集进行处理.许多图像文件名是利用纯数字递增的方式命名.通常所用的排序函数sort(),是按照 ...

  9. sqlserver结束和监视耗时的sql

    在对象资源管理器中右击服务器地址选择“活动和监视器”. 点击最近耗费大量资源的查询

  10. 团队作业4 Alpha冲刺《嗨!你的快递》

    仓库地址:https://git.coding.net/day_light/ourexpressmaster1.git   张新宇 1第一天日期:2018/6/13 1.1今日任务 进行核心功能数据匹 ...