第三章 前言 讨论 class的大小问题
#include"iostream"
using namespace std;
class X{};
class Y:public virtual X{};
class Z:public virtual X{};
class A:public Y,public Z{};
int main()
{
cout<<"X:"<<sizeof(X)<<endl;
cout<<"Y:"<<sizeof(Y)<<endl;
cout<<"Z:"<<sizeof(Z)<<endl;
cout<<"A:"<<sizeof(A)<<endl;
}
其继承关系为:
结果:
xiaobo@xiaobo-desktop:~/桌面/c++$ g++ model3.1.cpp -o model3.1
xiaobo@xiaobo-desktop:~/桌面/c++$ ./model3.1
X:1
Y:4
Z:4
A:8
以上结果不同的编译器会有不同,有的编译器会产生 1 ,8 , 8 , 12的结果
首先讨论第一种结果 VC和g++上的结果均为1,4,4,8,
X的大小为1,是因为编译器安插进去一个char,这样会使得这个class的两个object 在内存中的配置独一无二,
例如:
X a, b;
if(&a!=&b)cout << "different";
Y Z分别拥有一个指向virtual base class X subobject的指针 所以大小为4
A的大小为
内存布局如下图:
这类编译器把一个empty virtual base class视为derived class object的开头部分,不花费任何额外空间
这也证实了一个virtual base class subobject只会在derived class中存在一份实体,不管它在继承体系中出现了多少次。
有一类编译器会得出 1,8,8,12的结果 主要是因为它给空的Y,Z,A均加上了char ,这样Y,Z,A 分别为5,5,9,又编译器有Alignment机制会将数值调整到整数倍(31位机为4bytes的整数倍)所以为8,8,12。
3.2 Data Member Layout
using namespace std;
class X{
public:
void getAddress() const {cout<<&x1<<endl;
cout<<&m<<endl;
cout<<&x3<<endl;}
int getChunk() const {return chunkSize;}
private:
int x1; //
static int m;
static const int chunkSize = 10;
double x3;
};
#include"iostream"
#include"stdio.h"
using namespace std;
class X{
public:
void setA(int x){x1 = x;}
void setB(int x){ m = x ;}
void getAddress() const {cout<<&x1<<endl;
cout<<&m<<endl;
cout<<&x3<<endl;}
int getChunk() const {return chunkSize;}
private:
int x1;
static int m;
static const int chunkSize = 10;
double x3;
};
class Y:public virtual X{};
class Z:public virtual X{};
class A:public Y,public Z{};
int X::m = 1;
int main()
{
//X::m = 3;
cout<<"X:"<<sizeof(X)<<endl;
cout<<"Y:"<<sizeof(Y)<<endl;
cout<<"Z:"<<sizeof(Z)<<endl;
cout<<"A:"<<sizeof(A)<<endl;
X a,b;
a.setA(10);
a.setB(12);
a.setB(12);
if(&a!=&b)cout << "different"<<endl;
a.getAddress();
cout<<&a<<endl;
printf("---%p----",&a);
}
X:12
Y:16
Z:16
A:20
different
0xbf9bbfc4
0x804a038
0xbf9bbfc8
0xbf9bbfc4
---0xbf9bbfc4----x