C ++ type conversion Operators

Source: Internet
Author: User

Although C ++ is compatible with C, it also supports the type conversion usage of C language, that is, to enclose the type in parentheses. This conversion visibility is poor and it is difficult to track incorrect conversions. C ++ provides mandatory conversion operators for naming. After all, the conversion is sometimes insecure. Therefore, you can use the conversion operators to determine the risk level.

Next, let's take a look at the specific functions and usage of the four naming forced conversion operators provided by C ++.

1. const_cast

2. static_cast

3. reinterpret_cast

4. dynamic_cast

The usage of these four operators is the same, both cast-name <Type> (expression), and Type is the target Type of the converted value expression.

Next let's take a look at the application scenarios of these four operators respectively.

1. const_cast

As the name implies, const_cast removes the const attribute of the expression, for example

[Cpp]
Char * string_copy (char * s)
{
Return s;
}
Int main ()
{
Const char * pc_str;
Char * pc = string_copy (const_cast <char *> (pc_str); // a compilation error occurs if pc_str is directly input.
Return 0;
}
The above example only describes the problem and does not implement the function.

Remember that const_cast can only be used to add or delete a const attribute. You can only use const_cast to add or delete a feature.

2. static_cast

This function is widely used. Any type conversion implicitly executed by the compiler can be completed by static_cast.

In addition, static_cast is mostly used to forcibly convert a void * type pointer to the original pointer type. For example:


[Cpp]
Double d = 12;
Void * p = & d;
Double * pd = static_cast <double *> (p );
Cout <* pd <endl;
The output value is 12, indicating that the conversion still points to the same address.
3. reinterpret_cast

This operator is ugly and hard to remember. Why? It is designed to avoid it as much as possible because of its high risk. It is used to convert between pointers, for example:


[Cpp]
Int * ip address;
Char * pc = reinterpret_cast <char *> (ip );
String str (pc );
Although it can be compiled, it may cause serious runtime errors. Therefore, try to avoid the use of reinterpret_cast.
4. dynamic_cast

Dynamic_cast is used to convert the pointer or reference of the parent class to the pointer or reference of the Child class.

Dynamic_cast is different from the first three forced conversions. It involves runtime type checks. If the type object bound to the reference or pointer is not the target type object, dynamic_cast fails. When the conversion for pointer fails, dynamic_cast returns 0, and when the conversion for reference fails, a bad_cast exception is thrown.

Use the structure used in the following example to check the conversion:

Pointer conversion:


[Cpp]
If (Derived * derviedPtr = dynamic_cast <Derived *> (basePtr ){
// Use the Derived object to which derivedPtr points
}
Else {
// BasePtr oiubsts at a Base object
// Use the Base object to which basePtr points
}
Reference conversion:

[Cpp]
Void f (const Base & B)
{
Try {
Const Derived & d = dynamic_cast <const Derived &> (B );
// Use the Derived object to which B referred
}
Catch (bad_cast ){
// Handle the fact that the cast failed
}
}

Now, the conversion operator has been introduced.


By RO_wsy

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.