top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C++: What is the difference between dynamic and static casting?

+2 votes
652 views
C++: What is the difference between dynamic and static casting?
posted Dec 18, 2014 by Amit Kumar Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
 
Best answer

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. 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.

answer Dec 18, 2014 by Mohammed Hussain
...