一道关于继承类的sizeof大小问题
2008-10-15 21:01
447 查看
#include <iostream>
using namespace std;
struct A{
virtual int fun(); //4 byte
char x; //1 byte
};
struct B:public A{
short myfun(); //2 byte 不占类B空间
union{
unsigned short m;
unsigned int c;
}xx; //4 byte
int (__stdcall *funs)(); //4 byte
};
union{
unsigned short m;
unsigned int c;
}xx;
short myfun();
int (__stdcall *funs)(); //4 byte
struct C{
short myfun(); //2 byte
union{
unsigned short m;
unsigned int c;
}xx; //4 byte
int (__stdcall *funs)(); //4 byte
};
int main()
{
//cout<<endl;
//cout<<offsetof( B , x )<<endl;
//cout<<offsetof( B , xx )<<endl;
//cout<<offsetof( B , funs )<<endl;
cout < < sizeof(myfun()) < < endl;//2
cout < < sizeof(( *funs)()) < < endl;//4
cout < < sizeof(xx) < < endl;//4
cout < < sizeof(A) < < endl;//8
cout < < sizeof(B) < < endl;//16
cout < < sizeof(C) < < endl;//8
return 0;
}
//为啥sizeof(B)是16而不是18??offsetof可以看偏移
//看深入探索C++对象模型,类中非虚函数不占空间
using namespace std;
struct A{
virtual int fun(); //4 byte
char x; //1 byte
};
struct B:public A{
short myfun(); //2 byte 不占类B空间
union{
unsigned short m;
unsigned int c;
}xx; //4 byte
int (__stdcall *funs)(); //4 byte
};
union{
unsigned short m;
unsigned int c;
}xx;
short myfun();
int (__stdcall *funs)(); //4 byte
struct C{
short myfun(); //2 byte
union{
unsigned short m;
unsigned int c;
}xx; //4 byte
int (__stdcall *funs)(); //4 byte
};
int main()
{
//cout<<endl;
//cout<<offsetof( B , x )<<endl;
//cout<<offsetof( B , xx )<<endl;
//cout<<offsetof( B , funs )<<endl;
cout < < sizeof(myfun()) < < endl;//2
cout < < sizeof(( *funs)()) < < endl;//4
cout < < sizeof(xx) < < endl;//4
cout < < sizeof(A) < < endl;//8
cout < < sizeof(B) < < endl;//16
cout < < sizeof(C) < < endl;//8
return 0;
}
//为啥sizeof(B)是16而不是18??offsetof可以看偏移
//看深入探索C++对象模型,类中非虚函数不占空间
相关文章推荐
- 关于虚拟继承类的大小问题探索,VC++ 和 G++ 结果是有区别的
- 关于类对象大小的 sizeof 计算问题
- 关于sizeof与#pragma pack导致结构体大小变化的问题
- 关于类对象大小的 sizeof 计算问题
- 关于类对象大小的 sizeof 计算问题
- 关于类对象大小的 sizeof 计算问题(续)
- 关于sizeof的问题(2)——求结构体大小
- 转:struct 关于sizeof的大小-内存对齐问题
- 关于类对象大小的 sizeof 计算问题
- 关于c++中的public继承,private继承,以及protect继承的问题
- 简单问题:sizeof(char型数组)为数组大小,而不是4, 为什么? 100!
- 一道关于继承和多态的题目
- 关于163邮箱,上传附件,本地验证文件大小的问题。
- python一道关于颠倒字典中键值的问题
- [导入]关于petshop中simplepager(继承自Repeater)覆写DataSource的问题
- 关于sizeof的几个问题
- 关于原型链和继承问题的思考:为什么不能直接把父类的prototype赋值给子类的prototype
- 关于ios工程大小的问题
- 继承中关于基类私有方法的问题
- 关于继承NSDictionary之后出现 [NSArray count]: method only defined for abstract class的问题