当您尝试将 a 强制void*
转换为不是的东西时,C++ 是否会引发运行时异常?
class Sheep
{
public:
Sheep() { }
~Sheep() { }
void Bah()
{
// Print Bah!
}
};
class Unicorn
{
Unicorn() { }
~Unicorn() { }
void Stab(Sheep* s)
{
s->Bah();
}
};
int main()
{
Sheep sheep;
void* ptr = (void*) &s;
// I'm guessing this would be 'valid'
Unicorn* unicorn = (Unicorn*) ptr;
// This must go wrong..?
unicorn->Stab(&sheep);
return 0;
}