谁能给解释下这个问题,vc 6.0 和2005运行同一个程序的差异。
这个程序在vc 6.0,输出40,在vs 2005下为56,具体为什么呢?
不是很明白,那位给解释一下,非常感谢!
#include <iostream>
#include <string>
using namespace std;
class CAnimal
{
private:
float weight;
public:
CAnimal(float w)
{
weight=w;
}
void SetWeight(float w);
virtual void Speak()=0;
};
inline void CAnimal::SetWeight(float w)
{
weight=w;
cout < < "set weight CAnimal\n ";
}
class CLandAnimal:virtual public CAnimal
{
private:
float speed;
public:
CLandAnimal(float w, float sp ):CAnimal(w)
{
speed=sp;
}
void Speak()
{
cout < < "This is land anmial\n ";
}
};
class CWaterAnimal:virtual public CAnimal
{
private:
string name;
public:
CWaterAnimal(float w,string str):CAnimal(w)
{
name=str;
}
void Speak()
{
cout < < "This is Water anmial\n ";
}
};
class CFrog:public CLandAnimal,public CWaterAnimal
{
public:
CFrog(float w,float sp,string str):CAnimal(w),CLandAnimal(w,sp),CWaterAnimal(w,str)
{
}
void Speak()
{
cout < < "I am a frog\n ";
}
};
int main(void)
{
string str= "XiaoQiang ";
CFrog a(10.0,110.0,str);
cout < <sizeof(a) < <endl;
system( "pause ");
return 0;
}
[解决办法]
sizeof
跟编译器有关
这个问题可能有点复杂
随处都有讲sizeof的
我blog也有
[解决办法]
虚拟继承,呃
这个sizeof结果和编译器相关,不同的编译器大小可能不同,不同版本的也可能不同。