c++探索:对象大小

很多人看过《深度探索C++对象模型》,但不一定都能解释清楚下面的小程序,可以试试看。
不过无论你能不能解释,都不妨碍你使用C++.

#include <iostream>
using namespace std;
class a {};
class b{};
class c:public a{
virtual void fun()=0;
};

class d:public b,public c{virtual void fun();};

class e: public d{int test;};
void d::fun()
  {
  };
class f:public a{};
class g:public f{virtual void fun(){};};
class h:public a,public b{};
class i:public a,public b{char cc;char dd;char ee;char ff;void gao(){};};
class j:public a,public b{char cc;char dd;char ee;virtual void gao(){};};
class k:public a,public b{char cc;char dd;char ee;char ff;};
class l:public a{char cc;char dd;char ee;char ff;};
class m{char cc;char dd;char ee;char ff;};
class n{char nn;};
class o:public a,public b{};
class p:public n,public b,public a{char pp;};
class q{char qq;};
class r:public a,public n,public q{};
class s:public n,public q{};
class t:virtual public a, virtual public b{};
class u:virtual public b, virtual public c{};
class v:virtual public c{ char vv;};
class w:virtual public c{ char vv;};
class x:virtual public v, virtual public w{};
class y:virtual public v{ };
class z:virtual public y,virtual public x{ };

int main()
{
a aa;
b bb;

d dd;
e ee;
f ff;
g gg;
h hh;
i ii;
j jj;
k kk;
l ll;
m mm;
n nn;
o oo;
p pp;
q qq;
r rr;
s ss;
t tt;

a * pa=&aa;
b * pb=&bb;
d * pd=&dd;
e * pe=&ee;
f * pf=&ff;
g * pg=&gg;
h * ph=&hh;
i * pi=&ii;
j * pj=&jj;
k * pk=&kk;
l * pl=&ll;
m * pm = &mm;
n * pn =&nn;
o * po = & oo;
q * pq=&qq;
r * pr=&rr;
cout<<"sizeof(a)"<<sizeof(a)<<endl;
cout<<"sizeof(b)"<<sizeof(b)<<endl;
cout<<"sizeof(c)"<<sizeof(c)<<endl;
cout<<"sizeof(d)"<<sizeof(d)<<endl;
cout<<"sizeof(e)"<<sizeof(e)<<endl;
cout<<"sizeof(f)"<<sizeof(f)<<endl;
cout<<"sizeof(g)"<<sizeof(g)<<endl;
cout<<"sizeof(h)"<<sizeof(h)<<endl;
cout<<"sizeof(i)"<<sizeof(i)<<endl;
cout<<"sizeof(j)"<<sizeof(j)<<endl;
cout<<"sizeof(k)"<<sizeof(k)<<endl;
cout<<"sizeof(l)"<<sizeof(l)<<endl;
cout<<"sizeof(m)"<<sizeof(m)<<endl;
cout<<"sizeof(n)"<<sizeof(n)<<endl;
cout<<"sizeof(o)"<<sizeof(o)<<endl;
cout<<"sizeof(p)"<<sizeof(p)<<endl;
cout<<"sizeof(q)"<<sizeof(q)<<endl;
cout<<"sizeof(r)"<<sizeof(r)<<endl;
cout<<"sizeof(s)"<<sizeof(s)<<endl;
cout<<"sizeof(t)"<<sizeof(t)<<endl;
cout<<"sizeof(u)"<<sizeof(u)<<endl;
cout<<"sizeof(v)"<<sizeof(v)<<endl;
cout<<"sizeof(w)"<<sizeof(w)<<endl;
cout<<"sizeof(x)"<<sizeof(x)<<endl;
cout<<"sizeof(y)"<<sizeof(y)<<endl;
cout<<"sizeof(z)"<<sizeof(z)<<endl;
return   0;


}
不要看答案,自己写出来。答案在下一页。。。


































a 1
b 1
c 4
d 8
e 12
f 1
g 4
h 1
i 5
j 8
k 5
l  4
m 4
n  1
o 1
p  3
q  1
r  2
s  2
t  8
u 12
v 12
w 12
x 24
y 16
z 32

你可能感兴趣的:(c++探索:对象大小)