"C + + Primer Plus" 15.5 type conversion operator Learning notes

Source: Internet
Author: User
Tags volatile

C + + c More strictly restricts the allowable type conversions and adds 4 type conversion operators, which are more canonical in the conversion process:
* DYNAMIC_CAST;
* CONST_CAST;
* STATIC_CAST;
* Reinterpret_cast.
"Dynamic_cast Operator"
Assuming that high and low are two classes, while the types of ph and pl are high* and low*, the following statement assigns a low* pointer to PL only if low is the accessible base class for high (direct or indirect):
PL = Dynamic_cast<low *> ph;
Otherwise, the null pointer is assigned to PL. Typically, the syntax for this operator is as follows:
dynamic_cast < Type-name > (expression)
The purpose of this operator is to make it possible to convert upward in a class-like structure (because of the is-a relationship, such type conversions are safe), and no other transformations are allowed.
"Const_cast Operator"
The const_cast operator is used to perform a type conversion of only one purpose, that is, to change the value to const or volatile, whose syntax is the same as the dynamic_cast operator.
Const_cast < type_name > (expression)
If other aspects of the type are also modified, the above type conversion will fail. That is, the type of type_name and expression must be the same, except that the const or volatile feature (with or without) can be different. Again, assume that high and low are two classes:
High bar;
Const HIGH * Pbar = &bar;
...
High * PB = Const_castconst low * PL = const_cast<const low *> (pbar); Invalid
The first type conversion allows *PB to be called a pointer that modifies the value of the bar object, removing the const label. The second type conversion is illegal because it also attempts to change the type from const high* to const low*.
The reason for providing an operator is that sometimes you might need a value that is constant for most of the time and sometimes modifiable. In this case, you can declare the value as const and use const_cast when you need to modify it. This can also be achieved by type conversion, which can also change the type at the same time (meaning that const_cast cannot change the type at the same time):
High bar;
Const HIGH * Pbar = &bar;
...
High * PB = (high *) (Pbar); Valid
Low * PL = (Low *) (Pbar); Also valid
It is more secure to use the const_cast operator because it is possible to inadvertently both type and constant characteristics at the same time.
"Static_cast Operator"
The syntax of the static_cast operator is the same as other type conversion operators:
Static_cast < type_name > (expression)
The above conversion is legal only if type_name can be implicitly converted to the type that the expression belongs to, or expression can be implicitly converted to the type to which type_name belongs, otherwise an error occurs. Assuming that high is the base class for low, and that pond is an unrelated class, the conversion from high to low, the conversion from low to high is legal, and the conversion from low to pond is not allowed:
High bar;
Low blow;
...
High *PB = Static_castLow * PL = Static_cast<low *> (&bar); Valid downcast
Pond * Pmer = Static_cast<pond *> (&blow); Invalid, Pond unrelated
The first conversion is legal because the up conversion can be done explicitly. The second conversion is from the base class pointer to the derived class pointer, which cannot be performed without an explicit type conversion. The conversion of a type in another direction is possible without the need for a type conversion, so it is legal to use static_cast for a downward conversion.
Similarly, because no type conversions are required, the enumeration values can be converted to integers, so you can use static_cast to convert an integral type to an enumerated type. Similarly, you can use static_cast to convert a double to int, convert float to long, and various other numeric conversions.
"Reinterpret_cast operator"
The reinterpret_cast operator is used for inherently dangerous type conversions. It does not allow the deletion of const, but it does other annoying operations. Sometimes programmers have to do some implementation-dependent, annoying operations, and use the reinterpret_cast operator to simplify the tracking of this behavior. The syntax for this operator is the same as the other 3:
Reinterpret_cast < Type-name > (expression)
The following is an example of use:
struct DAT {short A; short b;};
Long value = 0xa224b118;
DAT * PD = reinterpret_cast< dat * > (&value);
cout << Hex << pd->a; Display first 2 bytes of value
In general, such conversions are suitable for implementation-dependent underlying programming techniques and are not portable. For example, when different systems store multibyte integers, the bytes in them may be stored in a different order.
However, the reinterpret_cast operator does not support all type conversions. For example, you can convert a pointer type to an integer that is sufficient to store the pointer representation, and a single cannot convert the pointer to a smaller integer or float type. Another limitation is that you cannot convert a function pointer to a data pointer, or vice versa.

In C + +, ordinary class conversions are also restricted. Basically, you can perform other types of conversions, plus some combinations, such as static_cast or reinterpret_cast followed by const_cast, but you cannot perform other transformations. Therefore, the following type conversions are allowed in the C language, but are not generally allowed in C + + because the char type is too small for most C + + implementations to store pointers:
char ch = char (&d); Type cast #2-convert address to a char

"C + + Primer Plus" 15.5 type conversion operator Learning notes

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.