c++ - bad_cast is not thrown always from dynamic_cast -


i'm facing issues while using exception handling dynamic_cast. not returning bad_cast always.

the statement below not throwing me bad_cast, though d1 returned nullptr , compiler showing me warning

derived *d1 = dynamic_cast <derived *> (&base); 

but if try following statement:

derived d1 = dynamic_cast <derived &> (base); 

then it throw std::bad_cast

am missing in logic? classes polymorphic.

this defined way.

if new type casted pointer result nullptr on error. if reference throws exception.

see http://en.cppreference.com/w/cpp/language/dynamic_cast

if cast successful, dynamic_cast returns value of type new_type. if cast fails , new_type pointer type, returns null pointer of type. if cast fails , new_type reference type, throws exception matches handler of type std::bad_cast.


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -