Hi friends! Whats the difference with
Reinterpret_cast
Dynamic_cast
dynamic_cast is done at runtime and is being used for casting pointers in class hierarchy. E.g. you have class B derived from A. Then you create B on the heap but store it as a pointer to A. Then you can use dynamic_cast to cast it back to pointer to B.
Dynamic cast can fail, if it fails it just returns nullptr.
reinterpret_cast is done at compile time and it's used to reinterpret some part of memory of one type as another type.
Reinterpret cast the is most unsafe cast (except for C-style cast), as it will not warn you if you break your memory. Also it implementation-defined, so you can have different results on different platforms (as the memory representation of the same types can vary).
So use static_cast or dynamic_cast instead of reinterpret_cast when possible.