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. appium自动化---activity获取

    方法一:appt查询activity获取 aapt dump badging <路径/包名> | find "launchable-activity" 方法二: 1.打 ...

  2. Quartz.net 定时任务在IIS中没有定时执行

    问题:Quartz.net 定时任务在项目部署到IIS中发现没有定时执行 解决方案: 1.在服务器上装一个360(自带自动刷新功能),在工具——>自动刷新——>自动刷新勾上 然后再设置一下 ...

  3. Android Service(下)

    阅读本文需要先阅读Android Service(上) 一 为什么需要bindService() 绑定服务就是为了和服务进行通讯 可以调用服务里面的方法 二 bindService()调用服务里面方法 ...

  4. AssetBundle压缩/内部结构/下载和加载

    一.AssetBundle的压缩方式   Unity支持三种AssetBundle打包的压缩方式:LZMA, LZ4, 以及不压缩.    1.LZMA压缩方式  是一种默认的压缩形式,这种标准压缩格 ...

  5. jar包冲突常用的解决方法

    jar包冲突常见的异常为找不到类(java.lang.ClassNotFoundException).找不到具体方法(java.lang.NoSuchMethodError).字段错误( java.l ...

  6. Ansible 连接主机显示报错的处理方案

    一.在ansible安装完毕后一般需要以SSH的方式连接到需要进行管理的目标主机,一开始遇到了如下问题: 192.168.15.4 | UNREACHABLE! => {    "ch ...

  7. Node.js文档和教程

    七天学会NodeJS:https://nqdeng.github.io/7-days-nodejs/ Node入门:http://www.nodebeginner.org/index-zh-cn.ht ...

  8. VS AI 手写数字识别应用

    看过我上一篇文章的朋友应该知道,我用VS训练出了minst模型.output目录中有.pb文件. 关于.pb文件的介绍见[参考链接] (https://stackoverflow.com/questi ...

  9. tcp/ip客户端与服务器

    单击“发送数据”把数据发送到指定IP地址的指定端口号 using System; using System.Collections.Generic; using System.ComponentMod ...

  10. jsp九大内置对象之session和application

    session和application 用的都是特别多尤其是application,但是想全面学习一下内置对象所以都了解一下. session又被称为是会话生存期是用户进入浏览器到关闭浏览器的期间.s ...