c++ - When dynamic_cast will throw exception in case used with pointer? -


i using dynamic_cast in source cast pointer thing below,

base *base = here storing pointer;  derived *derived = dynamic_cast<derived*>(base); 

in case of base doesn't have pointer of class hierarchy cast fails , returns null. in next lines checking null. no issues.

i came across crash dump, application crashed due dynamic_cast throws exception.

i know dynamic_cast throw when used reference types.

any idea when dynamic_cast can throw exception when used pointer used in above source?enter image description here

any idea when dynamic_cast can throw exception when used pointer used in above source?

in well-defined program, it cannot. standard not allow it:

[c++11: 5.2.7/9]: the value of failed cast pointer type null pointer value of required result type. failed cast reference type throws std::bad_cast (18.7.2).

however, if pass dynamic_cast invalid pointer, invoke undefined behaviour , anything may happen, including implementation-defined c++ exception, or runtime crash.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -