The Semantics of Data
来源:互联网 发布:蜂窝数据自动关闭 编辑:程序博客网 时间:2024/05/16 01:34
第三章 前言 讨论 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
- The Semantics of Data
- The Semantics of Data
- c++对象模型-the semantics of data
- Inside The C++ Object Model ---- The Semantics of Data
- The Semantics of Function
- C++对象模型(五):The Semantics of Data Data语义学
- Data语意学 - 类的大小应该是多少? (The Semantics of Data )
- Notes of “Quotient Cube: How to Summarize the Semantics of a Data Cube”
- inside the C++ object model chapter 3 the semantics of data
- 基本语言细节--《深度探索C++对象模型》--(3)The Semantics of Data--总结点
- 深度探索C++对象模型复习和学习 第三章 Data 语义学(The Semantics of Data )
- The Semantics of Constructors(构造语义学)
- c++对象模型-the semantics of constructors
- Inside The C++ Object Model ---- The Semantics of Constructors
- 《Inside C++ Object Model》 Chapter 2 : The semantics of Constructor
- what are the semantics of a const member function
- [文章摘要]Extracting semantics of individual places from movement data by analyzing temporal patterns of
- Move semantics of RapidJSON
- java中数据类型转换
- 制定一个计划吧,梦想是什么
- 解决MATLAB 图像处理中,blkproc 函数报错的问题
- ZJU题型分类
- 明白了这十个故事,你也就参悟了人生 !
- The Semantics of Data
- QT TableView 常用设置
- Delphi 的接口
- 构建高性能ASP.NET站点之一 剖析页面的处理过程(前端)
- qDebug() <<
- Google联合创始人Sergey Brin向维基百科捐款50万美元
- ARM汇编中子程序进入和退出时数据的保存和恢复
- 杭电ACM题目分类
- Cocos2d开发资源大集合