虚继承sizeof:继承类的sizeof大小问题



    道有关继承类大小问题起来看下吧:

# <iostream>
using std;
struct A{
virtual fun; //4
char x; //1

};
struct B:public A{
myfun; //2 不占类B空间
union{
unsigned m;
unsigned c;
}xx; //4
(__stdcall *funs); //4
};

union{
unsigned m;
unsigned c;
}xx;
myfun;
(__stdcall *funs); //4
struct C{
myfun; //2
union{
unsigned m;
unsigned c;
}xx; //4
(__stdcall *funs); //4
};

{
//cout<<endl;
//cout<<offof( B , x )<<endl;
//cout<<offof( B , xx )<<endl;
//cout<<offof( B , funs )<<endl;

cout < < (myfun) < < endl;//2
cout < < (( *funs)) < < endl;//4
cout < < (xx) < < endl;//4
cout < < (A) < < endl;//8
cout < < (B) < < endl;//16
cout < < (C) < < endl;//8
0;
}
//为啥(B)是16而不是18??offof可以看偏移
//看深入探索C对象模型类中非虚不占空间

Tags:  sizeofint sizeof sizeof空类 虚继承sizeof

延伸阅读

最新评论

发表评论