I've noticed with regard to the std::bad_cast exception that references and pointers don't seem to act the same way. For example:
class A { public: ~A() {} };
class B : public A {};
//Case #1
int main()
{
A a;
B& b = dynamic_cast<B&>(a); //Would throw std::bad_cast.
}
//Case #2
int main()
{
A* a = new A;
B* b = dynamic_cast<B*>(a); //Would not throw std::bad_cast.
}
In the first case, an exception of std::bad_cast is generated, and in the second case no exception is generated - instead, the b pointer just is assigned the value NULL.
Can someone explain to me why only the former throws an exception when both are bad_cast examples? I figure there's a good motive behind the decision, and that I'm misusing something as I don't understand that motivation.
Can someone explain to me why only the former throws an exception?
That is how dynamic_cast is specified to behave: a bad dynamic_cast involving pointers yields a null pointer, but there are no null references, so a bad dynamic_cast involving references throws a bad_cast.
The fact that a failed dynamic_cast involving pointers yields a null pointer is useful because it allows for cleaner, simpler type checking and allows for the following idiom:
if (B* b = dynamic_cast<B*>(a))
{
// The dynamic_cast succeeded and 'b' is non-null.
}
With this idiom, b is in scope and usable if and only if it is non-null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With