dynamic_cast and references Because C++ does not have a “null reference”, dynamic_cast can't return a null reference upon failure. Instead, if the dynamic_cast of a reference fails, an exception of type std::bad_cast is thrown.
dynamic_cast will no longer throw an exception when type-id is an interior pointer to a value type, with the cast failing at runtime. The cast will now return the 0 pointer value instead of throwing.
The static_cast operator converts a null pointer value to the null pointer value of the destination type. Any expression can be explicitly converted to type void by the static_cast operator. The destination void type can optionally include the const , volatile , or __unaligned attribute.
If it is, dynamic_cast<Type*> returns a pointer; otherwise it returns NULL.
§5.2.7/4:
If the value of v is a null pointer value in the pointer case, the result is the null pointer value of type R.
So you don't have to check for a null pointer yourself. Same goes for operator delete, deleting a null pointer has no effect.
Yes, you can use dynamic_cast
on a null pointer.
Yes, check 5.2.7.4 in standard.
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