I have always avoided the C++ casts (static_cast, const_cast, dynamic_cast [I also avoid RTTI], etc) because I regard them as a waste of typing and I never saw any advantages, so I use C-style casts exclusively.
My question is, if you have an inheritance hierarchy and a pointer to the base type, can you safely cast a base pointer to a derived pointer with a C-style cast (provided that somehow you are absolutely sure the base pointer points to an instance of a derived type) without something happening behind the scenes that will cause seemingly inexplicable failures?
I ask this because I read in one of the comments on another question that using a C-style cast from a base to a derived type will not “adjust the pointer” or something like that. I’ll try to find the exact comment again.
Using
static_castordynamic_castfor casting ensures some kind of safety.static_castgives you an compile time error if an cast is invalid while,dynamic_castthrows an exception for References or returns a null pointer in case of invalid cast at run time.Thus they are better than the c-style cast mainly due the safety they provide.
If you are absolutely sure of the cast being valid even a c-style cast just does the same, but it is better to use the language provided security than rather manage it ourself(since we don’t need to).