#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;
}
实验结果: