sizeof操作符关于类相关使用
来源:互联网 发布:北京美工培训 编辑:程序博客网 时间:2024/06/18 17:45
首先,看一个题目:以下代码使用VC编译成32位可执行程序,请问:nLenA、nLenAObject、nLenB、nLenAObject、nLenC、nLenCObject的值分别为
#include<iostream>#include<stdlib.h>using namespace std;class t_classA{public:t_classA(){};~t_classA(){};};class t_classB{public:t_classB(){};virtual ~t_classB(){};};class t_classC:public t_classA,public t_classB{public:t_classC(){};virtual ~t_classC(){};};int main(){int nLenA = sizeof(t_classA);cout<<"nLenA: "<<nLenA<<endl;t_classA oA;int nLenAObject = sizeof(oA);cout<<"nLenAObject: "<<nLenAObject<<endl;int nLenB = sizeof(t_classB);cout<<"nLenB: "<<nLenB<<endl;t_classB oB;int nLenBObject = sizeof(oB);cout<<"nLenBObject: "<<nLenBObject<<endl;int nLenC = sizeof(t_classC);cout<<"nLenC: "<<nLenC<<endl;t_classC oC;int nLenCObject = sizeof(oC);cout<<"nLenCObject: "<<nLenCObject<<endl;}
下面进行全面的分析:
1、空类
#include<iostream>#include<stdlib.h>using namespace std;class A//1{};int main(){A a;cout<<sizeof(A)<<endl;//1cout<<sizeof(a)<<endl;//1}
2、一般非空
#include<iostream>#include<stdlib.h>using namespace std;class A{int a;//4int *p;//4char c;//1};int main(){A a;cout<<sizeof(A)<<endl;//12cout<<sizeof(a)<<endl;//12}
3.有虚函数
#include<iostream>#include<stdlib.h>using namespace std;class A{public: A(){};virtual ~A(){};//4private: int b;//4 int *p;//4 char c;//1};int main(){A a;cout<<sizeof(A)<<endl;//16cout<<sizeof(a)<<endl;//16}
4.虚函数继承
#include<iostream>#include<stdlib.h>using namespace std;class A//16{public: A(){};virtual ~A(){};private: int b; int *p; char c;};class B:public A{public:B(){};~B(){};private: int b;//4};int main(){B b;cout<<sizeof(B)<<endl;//20cout<<sizeof(b)<<endl;//20}
5.静态成员函数
#include<iostream>#include<stdlib.h>using namespace std;class A//1{static int n;//0};int main(){A a;cout<<sizeof(A)<<endl;//1cout<<sizeof(a)<<endl;//1}
#include<iostream>#include<stdlib.h>using namespace std;class A//1{func();//0};int main(){A a;cout<<sizeof(A)<<endl;//1cout<<sizeof(a)<<endl;//1}
所以结果如下:
nLenA=1 nLenAObject=1 nLenB=4 nLenAObject=4nLenC=4nLenCObject=4
参考:http://blog.csdn.net/zzhongcy/article/details/38361755
阅读全文
0 0
- sizeof操作符关于类相关使用
- 关于C++的sizeof()操作符
- 《剑指offer》之关于sizeof()操作符
- 关于sizeof的相关问题
- sizeof操作符的使用详解
- sizeof操作符的使用详解
- sizeof使用的相关总结
- 关于sizeof操作符的理解和总结
- 总结:关于Sizeof的相关知识
- sizeof 操作符
- sizeof 操作符详解
- sizeof 操作符
- sizeof操作符
- 操作符sizeof
- sizeof操作符
- sizeof操作符
- [MSDN]sizeof 操作符
- sizeof 操作符
- Linux上.sh文件中的命令为什么执行不了?
- 122. Best Time to Buy and Sell Stock II
- sql语句的各种模糊查询
- 时间格式化、定时任务
- 父进程异常退出时,确保子进程退出
- sizeof操作符关于类相关使用
- ubuntu安装python-pip问题解决
- Python3读取Excel数据存入MySQL
- iOS block为什么要用copy
- 关于spring boot org.springframework.context.ApplicationContextException:Unable to start Embedded解决方案
- java代码面向对象实现:500人围成一圈,数3退一,求最后留下的人的最初位置
- linux下如何查看一个文件是dos格式还是unix格式的?
- 深度学习之----Dropout浅层理解与实现
- php 冒泡排序