Possible Duplicate:
Regular cast vs. static_cast vs. dynamic_cast
i’ve been using C-like casts since i’ve been programming:
class* initializedClassInstance;
void* test = (void*) initializedClassInstance;
and i’ve been told somewhere that i should get used to C++ casts (static_cast, dynamic_cast…).
Is there a reason to prefer one over the other (C++ over C style)?
There is a difference between static cast and dynamic cast, right? But what is it?
Thanks!
C-style casts are unsafe.
C++-style casts behave in another way.
static_castwill give you a compilation error if it can’t make the cast.dynamic_caston fail will cast to NULL if you are casting pointers, and throw an exception otherwise.So this allows you to write a safer code.