X
xllx.relient.xllx
Can anyone kindly explain why the following works, even though it's
undoubtedly a very bad thing to do:
class Bird
{
int age;
public:
Bird(int = 21);
void IFly(void);
};
Bird::Bird(int lage)
{
age = lage;
}
void Bird::IFly()
{
age = 21;
std::cout<<"Bird::IFly"<<std::endl;
std::cout<<"age: "<<age<<std::endl;
}
int main(int argc, char **argv)
{
int i;
Bird *x = reinterpret_cast<Bird*>(&i);
x->IFly();
return 0;
}
PRINTS: "Bird::IFly"
"age: 21"
undoubtedly a very bad thing to do:
class Bird
{
int age;
public:
Bird(int = 21);
void IFly(void);
};
Bird::Bird(int lage)
{
age = lage;
}
void Bird::IFly()
{
age = 21;
std::cout<<"Bird::IFly"<<std::endl;
std::cout<<"age: "<<age<<std::endl;
}
int main(int argc, char **argv)
{
int i;
Bird *x = reinterpret_cast<Bird*>(&i);
x->IFly();
return 0;
}
PRINTS: "Bird::IFly"
"age: 21"