派生类继承虚基类后对象的大小----C++学习(1)

原理

虚函数指针占内存的一个地址长度 32位则长度为4个字节 64位则长度为8个字节
1、当类初始化对象时,首先初始化基类,成员对象,最后当前类构造函数
2、如果基类中含有虚函数,基类构造时会初始化一个虚指针指向一个虚函数表
3、父类继承一个虚基类时,会复用基类的虚指针作为当前类的虚指针,并当前类中如果有基类虚函数的同名函数,则覆盖掉虚函数表中该函数的地址,使用当前类的函数地址。并且如果当前类含有基类不存在的虚函数,则向虚函数表中添加该虚函数地址
4、父类继承两个及以上的虚基类时,会复用所有虚基类的虚函数表,并且找到同名函数进行覆盖,并且将新的虚函数添加到第一个继承的基类的虚函数表中

1、基类有虚函数,派生类无虚函数

#include <iostream>
#include<vector>
#include<stack>
#include<algorithm>
#include<string>
#include<unordered_set>
using namespace std;

class A {
public:
	A(int x = 0) {
		cout << "A" << x << endl;
	}
	void printA() {
		cout << "Hello A";
	}
};
class B :public A{
public:
	B(int x = 0) {
		cout << "B" << x << endl;
	}
	virtual void printB() {
		cout << "Hello B";
	}
};

class C : public B{
public:
	C() {
		cout << "C" << endl;
	}
	void printC() {
		cout << "Hello C";
	}
};

int main()
{
	A a;
	B b;
	C c;
	cout << "size of a:" << sizeof(a) << endl;
	cout << "size of b:" << sizeof(b) << endl;
	cout << "size of c:" << sizeof(c) << endl;
	return 0;
}

在这里插入图片描述

2、基类有虚函数,派生类有虚函数

#include <iostream>
#include<vector>
#include<stack>
#include<algorithm>
#include<string>
#include<unordered_set>
using namespace std;

class A {
public:
	A(int x = 0) {
		cout << "A" << x << endl;
	}
	virtual void printA() {
		cout << "Hello A";
	}
};
class B :public A{
public:
	B(int x = 0) {
		cout << "B" << x << endl;
	}
	virtual void printB() {
		cout << "Hello B";
	}
};

class C : public B{
public:
	C() {
		cout << "C" << endl;
	}
	void printC() {
		cout << "Hello C";
	}
};

int main()
{
	A a;
	B b;
	C c;
	cout << "size of a:" << sizeof(a) << endl;
	cout << "size of b:" << sizeof(b) << endl;
	cout << "size of c:" << sizeof(c) << endl;
	return 0;
}

在这里插入图片描述

3、基类无虚函数,派生类无虚函数

空类:1个字节

4、基类无虚函数,派生类有虚函数

4或8个字节 一个指针的大小

5、两个基类都有虚函数,派生类无虚函数

#include <iostream>
#include<vector>
#include<stack>
#include<algorithm>
#include<string>
#include<unordered_set>
using namespace std;

class A {
public:
	A(int x = 0) {
		cout << "A" << x << endl;
	}
	virtual void printA() {
		cout << "Hello A";
	}
};
class B {
public:
	B(int x = 0) {
		cout << "B" << x << endl;
	}
	virtual void printB() {
		cout << "Hello B";
	}
};

class C : public B, public A {
public:
	C() {
		cout << "C" << endl;
	}
	void printC() {
		cout << "Hello C";
	}
};

int main()
{
	A a;
	B b;
	C c;
	cout << "size of a:" << sizeof(a) << endl;
	cout << "size of b:" << sizeof(b) << endl;
	cout << "size of c:" << sizeof(c) << endl;
	return 0;
}

在这里插入图片描述

6、两个基类都有虚函数,派生类有虚函数

#include <iostream>
#include<vector>
#include<stack>
#include<algorithm>
#include<string>
#include<unordered_set>
using namespace std;

class A {
public:
	A(int x = 0) {
		cout << "A" << x << endl;
	}
	virtual void printA() {
		cout << "Hello A";
	}
};
class B {
public:
	B(int x = 0) {
		cout << "B" << x << endl;
	}
	virtual void printB() {
		cout << "Hello B";
	}
};

class C : public B, public A {
public:
	C() {
		cout << "C" << endl;
	}
	virtual void printC() {
		cout << "Hello C";
	}
};

int main()
{
	A a;
	B b;
	C c;
	cout << "size of a:" << sizeof(a) << endl;
	cout << "size of b:" << sizeof(b) << endl;
	cout << "size of c:" << sizeof(c) << endl;
	return 0;
}

结果:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月光在发光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值