Possible Duplicate:
Regular cast vs. static_cast vs. dynamic_cast
I don't quite get when to use static cast and when dynamic. Any explanation please?
Possible Duplicate:
Regular cast vs. static_cast vs. dynamic_cast
I don't quite get when to use static cast and when dynamic. Any explanation please?
Use dynamic_cast
when casting from a base class type to a derived class type. It checks that the object being cast is actually of the derived class type and returns a null pointer if the object is not of the desired type (unless you're casting to a reference type -- then it throws a bad_cast
exception).
Use static_cast
if this extra check is not necessary. As Arkaitz said, since dynamic_cast
performs the extra check, it requires RTTI information and thus has a greater runtime overhead, whereas static_cast
is performed at compile-time.
In some contexts, like this one, "static" refers to compile-time and "dynamic" refers to run-time.
For compile-time checking, use static_cast (limited to what the compiler knows). For run-time checking, use dynamic_cast (limited to classes with RTTI). For no checking, use reinterpret_cast.
Dynamic cast requires RTTI and does some magic compared to static cast. static_cast is just a compile time cast, checks if origin class can be promoted to the casted class by some simple rules as inheritance.
For example, in cases of virtual inheritance only dynamic_cast can resolve the situation.
Apart, dynamic_cast will return NULL if the cast is not possible, so you can take a different decision.
In the other hand, dynamic_cast is slower, as it implies some code being executed, and as said previously, it needs RTTI enabled which increases the size of the binaries.
dynamic_cast checks information available at run-time, such as RTTI, it also traverses class hierarchies to see if such cast is possible.
Static casting is done by the compiler: it treats the result as the target type, no matter what. You do this when you're absolutely sure about the argument being of the target type.
Dynamic casting is done at runtime, and thus requires runtime type information. You do this when you're unsure about the type you have: the cast may fail, which is shown by the return value being null. It can also only be done for pointers and references.
static_cast
is similar to the old C style cast and can be applied to just about anything. static_cast
would be used when you certain of the types in question. For example, I usually use a static_cast
when casting between int
and enum
.
dynamic_cast
can only be used with pointers and references. On failure to cast, a null pointer is returned. dynamic_cast
is generally used when resolving pointers to classes used in inheritance where you want to make sure the pointer you are casting is of the expected type.
Also check out C++ : Documentation : C++ Language Tutorial : Type Casting
If you are talking about C++. Then static_cast is not safe type of casting. It can cast to your type but if its wrong it will not throw any error/message. So you will get bad object from that. And the dynamic_cast is throwing error if the casting failed :) Hope this helps ! :)