C++基础知识 - 虚函数表

单个类的虚函数表

#include <iostream>
#include <Windows.h>
using namespace std;

class Father {
public:
	virtual void func1() { cout << "调用虚函数Func1()" << endl; }
	virtual void func2() { cout << "调用虚函数Func2()" << endl; }
	virtual void func3() { cout << "调用虚函数Func3()" << endl; }
	void func4() { cout << "调用普通函数Func4()" << endl; }
public:
	int a = 10;	
	int b = 20;
};

//函数模板
typedef void(*type_t)(void);

int main(void) {
	Father father;
	
	cout << "father大小:" << sizeof(father) << endl;	//12字节
	
	cout << "father地址:" << &father << endl;

	cout << (int*)*(int*)(&father) << endl;

	//虚函数表指针, 取到对象的地址,再转成(int*),在访问里面的内容
	int* vptr = (int*)*(int*)(&father);
	
	cout << "调用第一个虚函数" << endl;
	((type_t)(*(vptr + 0)))();
	cout << "调用第二个虚函数" << endl;
	((type_t)(*(vptr + 1)))();
	cout << "调用第三个虚函数" << endl;
	((type_t)(*(vptr + 2)))();

	cout << "调用第一个数据成员" << endl;
	cout << "地址:" << &father.a << endl;
	cout << "值:"<< * (int*)((int)&father + 4) << endl;

	cout << "调用第二个数据成员" << endl;
	cout << "地址:" << &father.b << endl;
	cout << "值:" << *(int*)((int)&father + 8) << endl;

	system("pause");
	return 0;
}

执行效果:

在这里插入图片描述

VS的对象内存分布分析:
项目的命令行配置中添加: /d1 reportSingleClassLayoutFather
在这里插入图片描述
手绘内存分布:
在这里插入图片描述

  • 对象内,首先存储的是“虚函数表指针”,又称“虚表指针”。
    然后再存储非静态数据成员。
  • 对象的非虚函数,保存在类的代码中!
    对象的内存,只存储虚函数表和数据成员
    (类的静态数据成员,保存在数据区中,和对象是分开存储的)
  • 添加虚函数后,对象的内存空间不变!仅虚函数表中添加条目
    多个对象,共享同一个虚函数表!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值