源程序:
//基类与子类占用空间及字节对齐
#include<iostream>
using namespace std;
class BaseClass
{
int v1,v2;
char v4;
public:
int temp1(){}
};
class DerivedClass:public BaseClass
{
int v3;
int *p;
public:
int temp(){}
};
int main()
{
cout<<"Base="<<sizeof(BaseClass)<<endl;
cout<<"Derived="<<sizeof(DerivedClass)<<endl;
return 0;
}
//在32位系统中,指针的大小是4字节,而不是8字节
运行结果: