如何防止基类变量具有不同的值?将对象转换为基类会给我一个不同的值。
class BaseClass
{
int baseClassVariable;
BaseClass()
{
baseClassVariable = randint();
}
}
class Derived : public BaseClass
{
}
main()
{
Derived* D = new Derived();
BaseClass* bc = (BaseClass*)D;
//D->baseClassVariable is not the same as bc->baseClassVariable
print(D->baseClassVariable);
print(bc->baseClassVariable);
}
`