I often get confused about the different types of C++ casts: dynamic_cast can be used for “upcasting” or “downcasting”
1 |
Upcasting:Â Â Â A* pA = dynamic_cast<A*>(pB)Â when B inherits from A |
1 |
Downcasting:Â B* pB = dynamic_cast<B*>(pA)Â when B inherits from A |
static_cast static_cast conversions are not as safe as dynamic_cast conversions, because static_cast does no run-time type check, while dynamic_cast does. A dynamic_cast to an ambiguous pointer will fail, while a static_cast returns as if …