Static_cast ?. XML

Source: Internet
Author: User
Pre {Line-Height: 1; color: # 1e1e1e; Background-color: # d2d2d2; font-size: 16px ;}. sysfunc {color: #627cf6; font-style: italic; font-weight: bold ;}. selffuc {color: #800080 ;}. bool {color: # d2576f ;}. condition {color: #000080; font-weight: bold ;}. key {color: #000080 ;}. vaR {color: #800000; font-style: italic ;}. digit {color: # ff00ff; font-weight: bold ;}. includepre {color: # 1e1e1e ;}. operator? {Color: #008000; font-weight: bold ;}

static_cast

Usage: static_cast? <? Type-ID?>? (? Expression ?)

?

This operator converts expression to the Type-ID type, but does not check the runtime type to ensure the conversion security.

?

Note: static_cast cannot convert the const, volatile, or _ unaligned attribute of expression.

?

It has the following usage:

?

① It is used to convert the pointer or reference between a base class (parent class) and a derived class (subclass) in the class hierarchy.

?

It is safe to perform upstream conversion (convert the pointer or reference of a derived class to a base class;

?

When performing a downstream conversion (converting a base class pointer or reference to a derived class), it is not safe because there is no dynamic type check.

?

② It is used for conversion between basic data types. For example, convert int to Char and convert int to enum. The security of such conversions must also be ensured by developers.

?

③ Convert a null pointer to a null pointer of the target type.

?

④ Convert any type of expression to void type.

?? What is the difference between static_cast and reinterpret_cast in C ++? In chapter 5 of C ++ primer, the compiler implicitly executes any type conversion, which can be displayed by static_cast. reinterpret_cast usually provides a lower-level re-interpretation for the bit mode of the operand?

1. static_cast in C ++ performs non-polymorphism conversion instead of the normal conversion operation in C. Therefore, it is used as an implicit type conversion. For example:

int?i;float?f?=?166.71;i?=?static_cast<int>(f);

In this case, the result is that the I value is 166.

?

2. reinterpret_cast in C ++ converts data from one type to another. The so-called "usually provides lower-level re-interpretation for the bit mode of the operand" means re-interpretation of the data in the form of binary existence. For example:

??int?i;char?*p?=?"This?is?a?example.";i?=?reinterpret_cast<int>(p);?

In this case, the values of I and P are exactly the same. The purpose of reinterpret_cast is to interpret the value of P as an integer in binary (bit mode) mode and assign it to I, // I? It is also a pointer, an integer pointer. An obvious phenomenon is that there is no digital loss before and after conversion.

Used in this article? Calligraphy and painting novel software? Released, the content has nothing to do with the software, and the painting and calligraphy novel software? More comfortable reading, more comfortable writing, and easier Publishing.

Static_cast ?. XML

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.