根据我阅读的内容,执行错误的运行时 dynamic_cast 可能会引发 bad_cast 异常或返回零。
如果你正在转换指针,它会返回零是否正确?
IE:
class Base { virtual void a(){} };
class Derived: public Base {};
int main () {
Base *base = new Base();
dynamic_cast<Derived*>(base);
return 0;
}
并且在转换对象时会抛出 bad_cast 异常?
IE:
class Base { virtual void a(){} };
class Derived: public Base {};
int main () {
Base base;
Base& ref = base;
dynamic_cast<Derived&>(ref);
return 0;
}