C++各种类型变量所占大小

#include <iostream>
#include <string>
using namespace std;
class A{
};
class A2{
};
class B : public A{
};
class C : public virtual A {
};
class D : public A, public A2 {
};
class Base {
public:
	int a;
	char b;  //字节对齐,占用4个字节
	static c; //存放在静态存储区,sizeof只能计算所占栈区的大小
	virtual f(){ cout<<"virtual fun"<<endl; } //虚指针
};
class Derive : public Base {
public:
	int d;
}; //要包括子类大小
class Derive1 : public virtual Base {
public:
	int d;
}; //要包括子类大小和指向基类指针
int main()
{
	printf("32位主机各个类型占用大小\n");
	printf("sizeof bool is %d,\nsize of char is %d,\nsize of short is %d,\nsize of short int is %d,\n\
size of int is %d,\nsize of long int is %d,\nsize of float is %d,\nsize of long is %d,\nsize of double is %d,\n\
size of long double is %d,\nsize of pointer is %d\n,size of string is %d\n,size of string point is %d\n",sizeof(bool),sizeof(char),sizeof(short),sizeof(short int), sizeof(int),\
		sizeof(long int),sizeof(float),sizeof(long),sizeof(double),sizeof(long double),sizeof(int *),sizeof(string),sizeof(string *));
	printf("size of empty class A is %d,\nsize of empty class B is %d,\nsize of empty class C is %d,\nsize of empty class D is %d\n",sizeof(A), sizeof(B), sizeof(C), sizeof(D)); 
	Base *base=new Derive; 
	printf("size of Base class is %d,\nsize of Derive class is %d,\nsize of Base pointer is %d,\nsize of Derive1 is %d\n",sizeof(Base),sizeof(Derive),sizeof(base),sizeof(Derive1));
	return 1;
}

实验结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值