Suppose I'm given a C++ library full of inheritance. I'm given a Base* in a function when I know that it is actually pointing to a Derived object and Derived inherits Base. But I don't know what kind of inheritance it is (public/protected/private). I also don't know if there is any virtual function in the hierarchy.
Given this situation, without looking into the source code/documentation of Base and Derived, which cast should I use? Or should I consult the code/documentation first to ensure about polymorphism?
Background
I am writing changeEvent function of QMainWindow in Qt 4.7. The changeEvent function takes QEvent* which I can cast to other type by knowing QEvent::type(). I was wondering if I should use static_cast or dynamic_cast.
Thanks.
Use static_cast. If you know that your Base* points to a Derived, then use static_cast. dynamic_cast is useful for when it might point to a derived.
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