鬼魂力量遗产s武将:C++关于内存单元的问题(高手近来)

来源:百度文库 编辑:高校问答 时间:2024/05/12 01:26:25
#include<iostream.h>
#pragma pack(1)
class A
{
public:
A(int x=65,int y=65)
{
m=x;
n=y;
cout<<this<<"------this--A"<<endl;
cout<<&m<<"-------&m---A"<<endl;
cout<<&n<<"-------&n---A"<<endl;
}
protected:
int m;
int n;
};
class B:virtual public A
{
public:
B(int x,int y)
{
m1=x;
cout<<this<<"------this--B"<<endl;
cout<<&m1<<"-------&m1---B"<<endl;
}
protected:
int m1;
};
class C: virtual public A
{
public:
C(int x,int y):A(x,y)
{
m2=x;
cout<<this<<"------this--C"<<endl;
cout<<&m2<<"-------&m2---C"<<endl;

}
protected:
int m2;
};
class D:public B,public C
{
public:
D(int x,int y):B(x,y),C(x,y)
{
m3=x;
cout<<this<<"------this--D"<<endl;
cout<<&m3<<"-------&m3---D"<<endl;
}
protected:
int m3;
};
void main()
{
D haha(34,54);
cout<<&haha<<"main()--------&haha"<<endl;
}
#include<iostream.h>
#pragma pack(1)
class A
{
public:
A(int x=65,int y=65)
{
m=x;
n=y;
cout<<this<<"------this--A"<<endl;
cout<<&m<<"-------&m---A"<<endl;
cout<<&n<<"-------&n---A"<<endl;
}
protected:
int m;
int n;
};
class B:virtual public A
{
public:
B(int x,int y)
{
m1=x;
cout<<this<<"------this--B"<<endl;
cout<<&m1<<"-------&m1---B"<<endl;
}
protected:
int m1;
};
class C: virtual public A
{
public:
C(int x,int y):A(x,y)
{
m2=x;
cout<<this<<"------this--C"<<endl;
cout<<&m2<<"-------&m2---C"<<endl;

}
protected:
int m2;
};
class D:public B,public C
{
public:
D(int x,int y):B(x,y),C(x,y)
{
m3=x;
cout<<this<<"------this--D"<<endl;
cout<<&m3<<"-------&m3---D"<<endl;
}
protected:
int m3;
};
void main()
{
D haha(34,54);
cout<<&haha<<"main()--------&haha"<<endl;
}
结果大家自己看看
我问的是为什么类B和类C后面有4个字节的储存单元(VC6.0)没运用