数据结构:结构体在继承中数据内存排列探究

本文探讨了在VS2013(MS VC++ 12.0)和gcc 3.4.6中,结构体继承时内存排列的特性。在VS2013环境下,父结构体在前,子结构体在后,不采用紧凑排列。而在gcc环境下,同样遵循父结构体成员在前的原则,但不同编译器间的紧凑排列策略可能有差异。

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


下面以一个例子来说明(为了方便,结构体命名方面就马虎点了):
#ifndef _STRUCTTEST_H_
#define _STRUCTTEST_H_

typedef struct tag_Base
{
	int s32Count;
	char byNone;
	bool bYes;
	long dwTemp;
	tag_Base()
	{
		s32Count = 100;
		byNone = 'Y';
		bYes = true;
		dwTemp = 200;
		printf("Base created!\n");
	};
}TBase;

typedef struct tag_Base2
{
	int s32Count;
	char byNone;
	long dwTemp;
	bool bYes;
	tag_Base2()
	{
		s32Count = 100;
		byNone = 'Y';
		bYes = true;
		dwTemp = 200;
		printf("Base2 created!\n");
	};
}TBase2;


typedef struct tag_InheritBase : public TBase
{
	char byIyes;
	int dwItemp;
	tag_InheritBase()
	{
		byIyes = '0';
		dwItemp = 1023;
		printf("Tinherit has been created!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值