Ultimate Analysis of dynamic_cast and static_cast

Source: Internet
Author: User
Static_cast: cast type conversion. Therefore, it can certainly be used for cast between classes with inheritance relationships. There are three types of subdivisions:
Upcast: Just same as dynamic_cast. Because the runtime type check is not required, the efficiency is higher than that of dynamic_cast;
Downcast: Not safe. It is not recommended.
Crosscast: unavailable. Compilation error.

Dynamic_cast: Cast between classes with inheritance relationships. It is very safe, but it requires a runtime type check and virtual table support, which is less efficient. Category 3:
Upcast: OK. Static_cast is enough.
Downcast: it is required. In this way, the NULL pointer can be obtained only when downcast is incorrect, instead of a non-null pointer that can be used as static_cast.
Crosscast: it can only be used.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.