C++实例(继承类的sizeof大小问题) |
|
www.nanhushi.com 佚名 不详 |
一道关于继承类的sizeof大小问题,考试大提示: 一起来看一下吧! #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++对象模型,类中非虚函数不占空间 考试大(www.Examda。com)
|
|
|
文章录入:杜斌 责任编辑:杜斌 |
|
上一篇文章: C++实例(去除数组中的重复数字) 下一篇文章: C++字节顺序(bigendian&littleendian) |
【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |
|
|