【发布时间】:2011-04-09 17:37:30
【问题描述】:
当您尝试将 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;
}
【问题讨论】:
-
@jfs:我在 Windows 上运行了这种场景,它没有抱怨。我想知道它是否是已定义的行为。
标签: c++ polymorphism void-pointers