I still wonder why the following gives std::bad_cast
exception
#include <typeinfo>
class A {virtual void fun() {}};
class B : public A {};
int main() {
try {
A a;
B b = dynamic_cast<B&>(a);
} catch (std::bad_cast& e) {
std::cerr << e.what() << '\n';
}
}
class A
is a polymorphic class
class B
is publicly derived from A
properly doing a downcast from base class A
object through reference, dynamic_cast
does accept a reference argument
but still why this exception?