求高手解答类中各种sizeof问题
下面是各种类中的sizeof问题,纯属好奇,求解答,涉及虚函数、虚继承、数据对齐问题;
其实还有好多不解的问题,先列以下这几种吧,求高手解答~~~
View Code
1 #include<iostream> 2 using namespace std; 3 class a{virtual void f(){} double av;int ab; 4 //virtual void f1(){} 5 }; 6 class b{//int aa;int aad; 7 //virtual void f1(){} 8 }; 9 class c{//int aad 10 ;}; 11 class d{int aad; 12 }; 13 class e:public virtual a//,public virtual bublic virtual c,public virtual d 14 {}; 15 void main() 16 {cout<<sizeof(e)<<endl;}
输出是32;
View Code
1 #include<iostream> 2 using namespace std; 3 class a{virtual void f(){} double av;int ab; 4 //virtual void f1(){} 5 }; 6 class b{//int aa;int aad; 7 //virtual void f1(){} 8 }; 9 class c{//int aad 10 ;}; 11 class d{int aad; 12 }; 13 class e:public virtual a,public virtual b,public virtual c//,public virtual d 14 {}; 15 void main() 16 {cout<<sizeof(e)<<endl;}
输出是 36;
View Code
1 #include<iostream> 2 using namespace std; 3 class a{virtual void f(){} double av;int ab; 4 //virtual void f1(){} 5 }; 6 class b{//int aa;int aad; 7 virtual void f1(){} 8 }; 9 class c{//int aad 10 ;}; 11 class d{int aad; 12 }; 13 class e:public virtual a,public virtual b,public virtual c//,public virtual d 14 {}; 15 void main() 16 {cout<<sizeof(e)<<endl;}
输出是36;
View Code
1 #include<iostream> 2 using namespace std; 3 class a{virtual void f(){} double av;int ab; 4 //virtual void f1(){} 5 }; 6 class b{int aa;int aad; 7 virtual void f1(){} 8 }; 9 class c{//int aad 10 ;}; 11 class d{int aad; 12 }; 13 class e:public virtual a,public virtual b,public virtual c//,public virtual d 14 {}; 15 void main() 16 {cout<<sizeof(e)<<endl;}
输出是44;
View Code
1 #include<iostream> 2 using namespace std; 3 class a{virtual void f(){} double av;int ab; 4 //virtual void f1(){} 5 }; 6 class b{int aa;int aad; 7 virtual void f1(){} 8 }; 9 class c{//int aad; 10 virtual void f2(){} 11 }; 12 class d{//int aad; 13 }; 14 class e:public virtual a,public virtual b,public virtual c,public virtual d 15 {}; 16 void main() 17 {cout<<sizeof(e)<<endl;}
输出是48;