Some static_cast const_cast

Source: Internet
Author: User

Static_cast, the forced type conversion operator in the C syntax can also be completed. But then again, what the forced type conversion in C can do is not always done, for example, remove the const.
In addition, it is declared that the average person will not be aware of it, that is, only one of the four can be used for non-pointer non-reference type conversion, the other three operators are basically dealing with pointers or references.
Const_static: remove the const, volatile, and _ unaligned attributes of "Pointer" or "Reference. Many people think that using this operator can make const something so "solid" as a false one. In fact, it is not the case. In essence, it can only remove a layer of false shell for you, if the things that are "pointed" or "referenced" are const itself, it is futile for you to make too much effort. It is generally used in such a set of logic:
Int A = 34; const int * pcint = & A; int * pint = const_cast <int *> (pcint); * pint = 0; in this way, the const Pointer "This pointer requires that it cannot be changed to the stuff it points to" is modified. If it is changed to const int A = 34 here, although the compilation still works, the operation is still OK, but when you use * pint to modify it, you will get the modified value: A is still 34, and * pint is indeed 0, however, it is interesting that pint = & A is still valid at this time. Although pint points to a, its value is not equal to. So far, I hope you will not misuse const_cast in the future.
Dynamic_cast: In a refined language, it is specifically used for down cast in object oriented. However, there are three prerequisites: first, it must be used for conversion between classes and subclasses; second, it must be used for conversion of pointers or reference types; and third, the base class must have virtual functions.
Reinterpret_cast: Used to reencapsulate pointers, that is, convert between pointer classes. In addition, it can be used to convert between pointer types and unsigned int types.

Some static_cast const_cast

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.