Staitc_cast, const_cast ....

Source: Internet
Author: User
# Include <iostream> using namespace STD; int main () {// 1. const_cast // const int A = 10; // int * PTR = const_cast <int *> (& A); // remove the const attribute // * PTR = 100; // 2. static_cast // The Operations implicitly executed by the compiler can be displayed by static_cast. The displayed forced type conversion generates a warning, when static_cast is used, the warning is disabled. // static_cast is the basic type conversion. Double dnum = 12.2356; void * PTR = & dnum; // double * dp = static_cast <double *> (PTR); // This is the c ++ standard format double * dp = (double *) PTR; // forced conversion in C language, with the same effect as the previous sentence // 3.r Einterpret_cast I want to see what this is. // re-Explain it based on the binary bit to see if it makes no sense. // 4. dynamic_cast has time to look at getchar (); Return 0;} // the example of a difference between static_cast and reinterpret_cast is to map to a completely different type, this keyword is used when we need to map the type back to the original type. The type we map to is only for xuanjicang and other purposes, which is the most dangerous of all mappings. (This is the original statement in the C ++ programming philosophy) the difference between static_cast and reinterpret_cast lies in multiple inheritance, such as class A {public: int M_a;}; Class B {public: int m_ B ;}; Class C: Public A, public B {}; then for the following code: C; printf ("% P, % P, % P", & C, reinterpret_cast <B *> (& C), static_cast <B *> (& C); the first two output values are the same, the last one offsets four bytes from the original base. This is because static_cast calculates the offset of parent-child pointer conversion and converts it to the correct address (M_a, m_ B, convert to B * pointer and then point to m_ B), but reinterpret_cast does not perform this conversion. Therefore, use reinterpret_cast with caution.

 

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.