No, if parent has a QWidget* runtime type, then static_cast<QWidget*>(parent) is clearly defined and does what you expect. If it is not, then the behavior is undefined.
Contrast with dynamic_cast , which has always determined behavior, but is less efficient since it should use information such as runtime.
A good way to safely reinstall in debug mode and quickly in release mode is, for example:
template <typename T, typename U> T* down_cast(U* x) { #ifdef NDEBUG return static_cast<T*>(x); #else return &dynamic_cast<T&>(*x); // Thanks @Martin #endif }
used as follows:
QWidget* w = down_cast<QWidget*>(parent);
Alexandre C.
source share