unexpected static_cast behavior

Hi everyone,

When a dynamic_cast<A*>(b) returns NULL, doesn't that mean a static_cast<A*>(b) should throw an exception?
I'm using wxWidgets, and I accidentally converted a wxChoice to a wxTextCtrl using a static cast. However, it did not complain, and the program just worked. Can someone explain why it doesn't crash?

For completeness, this is the code that works even though it->second->input_field is of type wxChoice.

static_cast<wxTextCtrl*>(it->second.input_field)->SetValue(child->GetStringValue());

To be clear: dynamic_cast<wxTextCtrl*>(it->second.input_field) returns NULL.
static_cast never throw exceptions. The only safety static_cast has is that it gives an error at compile time for casts that are obviously not safe like converting an int* to a bool*, but otherwise it's you as a programmer that is responsible to know what you are doing and if you do an invalid conversion that would result in undefined behaviour, which means there are no guarantees what will happen. The program might crash, it might act buggy, or it might just "work".
Last edited on
Topic archived. No new replies allowed.