对象内存布局 (16)

本文探讨了C++中虚基类与虚函数并存时对象内存布局的变化,通过具体示例代码展示了不同类层次结构下虚函数表与虚基类表的位置关系及其对对象内存布局的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前篇:http://blog.youkuaiyun.com/pathuang68/archive/2009/04/24/4105902.aspx

 

下面讨论虚基类和虚函数同时存在的时候,对对象内存布局的影响。

假定各个类之间的关系如下图:

  

Base中声明了一个虚函数vfBase()和一个整形成员变量;

Derived1 override了Base中声明的虚函数vfBase(),声明了一个虚函数vfDerived1(),另有一个整形成员变量derived1_member;

Derived2 override了Base中声明的虚函数vfBase(),声明了一个虚函数vfDerived2(),另有一个整形成员变量derived2_member;

ChildDerived分别override了Base、Derived1和Derived2中声明的虚函数vfBase()、vfDerived1() 和vfDerived2(),另外有一个整形成员变量childderived_member;

 

代码如下:

#include <iostream>

using namespace std;

 

typedef void (*VFun)(void);

 

template<typename T>

VFun virtualFunctionPointer(T* b, int i)

{

         return (VFun)(*((int*)(*(int*)b) + i));

}

 

template<typename T>

int virtualBaseTableOffset(T* b, int i)

{

         return (int)*((int*)*(int*)b + i);

}

 

class Base

{

public:

         int base_member;

         inline virtual void vfBase()

         {

                   cout << "This is in Base::vfBase()" << endl;

         }

};

 

class Derived1 : public virtual Base

{

public:

         int derived1_member;

         inline void vfBase()

         {

                   cout << "This is in Derived1::vfBase()" << endl;

         }

         inline virtual void vfDerived1()

         {

                   cout << "This is in Derived1::vfDerived1()" << endl;

         }

};

 

class Derived2 : public virtual Base

{

public:

         int derived2_member;

         inline void vfBase()

         {

                   cout << "This is in Derived2::vfBase()" << endl;

         }

         inline virtual void vfDerived2()

         {

                   cout << "This is in Derived2::vfDerived2()" << endl;

         }

};

 

class ChildDerived : public Derived1, public Derived2

{

public:

         int childderived_member;

         inline void vfBase()

         {

                   cout << "This is in ChildDerived::vfBase()" << endl;

         }

         inline void vfDerived1()

         {

                   cout << "This is in ChildDerived::vfDerived1()" << endl;

         }

         inline void vfDerived2()

         {

                   cout << "This is in ChildDerived::vfDerived2()" << endl;

         }

};

 

int main(void)

{

         ChildDerived cd;

         VFun pVF;

         int* tmp;

 

         cout << "sizeof(Base) = /t/t" << sizeof(Base) << endl;

         cout << "sizeof(Derived1) = /t" << sizeof(Derived1) << endl;

         cout << "sizeof(Derived2) = /t" << sizeof(Derived2) << endl;

         cout << "sizeof(ChildDerived) = /t" << sizeof(ChildDerived) << endl;

        

cout << endl;

         cout << "address of ChildDerived object:" << endl;

         cout << "address = " << (int*)&cd << endl;

        

cout << endl;

         cout << "1st virtual function table: " << endl;

         pVF = virtualFunctionPointer(&cd, 0);

         pVF();

 

         cout << endl;

 

         cout << "1st virtual base table: " << endl;

         tmp = (int*)((int*)&cd + 1);

         cout << "address = " << tmp << endl;

         cout << virtualBaseTableOffset(tmp, 0) << "/t<- not sure yet, but it doesn't matter here." << endl;

         cout << virtualBaseTableOffset(tmp, 1) << "/t<- offset from Derived1 subobject's vbptr to Base subobject." << endl;

         cout << virtualBaseTableOffset(tmp, 2) << "/t<- means the end of this virtual base table."  << endl;

 

         cout << endl;

 

         tmp = ((int*)&cd) + 3;

         cout << "2nd virtual function table: " << endl;

         pVF = virtualFunctionPointer(tmp, 0);

         pVF();

 

         cout << endl;

 

         cout << "2nd virtual base table: " << endl;

         tmp = (int*)((int*)&cd + 4);

         cout << "address = " << tmp << endl;

         cout << virtualBaseTableOffset(tmp, 0) << "/t<- not sure yet, but it doesn't matter here." << endl;

         cout << virtualBaseTableOffset(tmp, 1) << "/t<- offset from Derived2 subobject's vbptr to Base subobject."  << endl;

         cout << virtualBaseTableOffset(tmp, 2) << "/t<- means the end of this virtual base table."  << endl;

 

         cout << endl;

 

         tmp = ((int*)&cd) + 7;

         cout << "3rd virtual function table: " << endl;

         pVF = virtualFunctionPointer(tmp, 0);

         pVF();

 

         cout << endl;

 

         cout << "Derived1 subobject address = /t" << (Derived1*)&cd << endl;

         cout << "Derived2 subobject address = /t" << (Derived2*)&cd << endl;

         cout << "Base subobject address = /t" << (Base*)&cd << endl;

         return 0;

}

运行结果如下:

 

ChildDerived、Derived1和Derived2对象memory layout分别图解如下:

 

两个虚基类偏移量图解如下(虚函数表和虚基类表略):

 

结论:

其一,只要涉及到虚基类,一切问题就变得复杂起来;

其二,如果同时存在vfptr和vbptr,vfptr居前,vbptr居后;

其三,普通基类居前,虚基类总是尽可能地排列在layout的最后;

其四,两个同一层次的虚基类subobject,先声明者居前,后声明者居后,这点和普通基类是一样的;

其五,两个不同层次的虚基类subobject,层次高者居前,层次低者居后;

其六,Stan Lippman建议,不要在一个virtual base class中声明nonstatic data member,理由是这样做会是问题变得非常复杂。 

资源下载链接为: https://pan.quark.cn/s/d9ef5828b597 在本文中,我们将探讨如何通过 Vue.js 实现一个带有动画效果的“回到顶部”功能。Vue.js 是一款用于构建用户界面的流行 JavaScript 框架,其组件化和响应式设计让实现这种交互功能变得十分便捷。 首先,我们来分析 HTML 代码。在这个示例中,存在一个 ID 为 back-to-top 的 div 元素,其中包含两个 span 标签,分别显示“回到”和“顶部”文字。该 div 元素绑定了 Vue.js 的 @click 事件处理器 backToTop,用于处理点击事件,同时还绑定了 v-show 指令来控制按钮的显示与隐藏。v-cloak 指令的作用是在 Vue 实例渲染完成之前隐藏该元素,避免出现闪烁现象。 CSS 部分(backTop.css)主要负责样式设计。它首先清除了一些默认的边距和填充,对 html 和 body 进行了全屏布局,并设置了相对定位。.back-to-top 类则定义了“回到顶部”按钮的样式,包括其位置、圆角、阴影、填充以及悬停时背景颜色的变化。此外,与 v-cloak 相关的 CSS 确保在 Vue 实例加载过程中隐藏该元素。每个 .page 类代表一个页面,每个页面的高度设置为 400px,用于模拟多页面的滚动效果。 接下来是 JavaScript 部分(backTop.js)。在这里,我们创建了一个 Vue 实例。实例的 el 属性指定 Vue 将挂载到的 DOM 元素(#back-to-top)。data 对象中包含三个属性:backTopShow 用于控制按钮的显示状态;backTopAllow 用于防止用户快速连续点击;backSeconds 定义了回到顶部所需的时间;showPx 则规定了滚动多少像素后显示“回到顶部”按钮。 在 V
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值