Four types of conversion in C ++

Source: Internet
Author: User

There are c-style conversions, and C ++-style conversions. The C-style conversion format is simple.(Type) expression,However, C-style type conversion has many disadvantages. Sometimes it is inappropriate to use C-style conversion because it can be converted between any types, for example, you can convert a pointer to a const object to a pointer to a non-const object, and convert a pointer to a base class object to a pointer to a derived class object, the difference between the two conversions is huge, but the traditionalC LanguageThe type conversion of the style is not differentiated. Another drawback is that C-style conversion is not easy to find. It consists of a bracket and an identifier.Program.Therefore, C ++ introduced 4 new type conversion operators to overcome these disadvantages. They are 1. static_cast 2. const_cast 3. dynamic_cast 4. reinterpret_cast.

1.Static_cast

The most common type conversion operator, which is a type conversion under normal conditions, such as converting int to float, such as: int I; float F; F = (float) I; or F = static_cast <float> (I );

2.Const_cast

Used to retrieve the const attribute and change the const type pointer to a non-const type pointer, for example, const int * Fun (int x, int y) {} int * PTR = const_cast <int *> (fun (2.3 ))

3.Dynamic_cast

This operator is used to check whether the conversion type is secure at runtime, but is valid only when the polymorphism type is used, that is, this class has at least one virtual method. Dynamic_cast has the same basic syntax as static_cast. dynamic_cast is mainly used for upstream and downstream conversions between classes, and can also be used for cross conversions between classes. When performing upstream conversion between classes, dynamic_cast and static_cast have the same effect. During downstream conversion, dynamic_cast has the type check function, which is safer than static_cast.For example:

Class C

{
//... C. No virtual functions
};
Class t {
//...
}
Int main ()
{
Dynamic_cast <t *> (New C); // Error
}
It is legal to change it to the following:
Class C

{
Public:
Virtual void M () {}; // C is now Polymorphism
}

4.Reinterpret_cast

Interpret indicates the meaning of interpretation. reinterpret indicates a re-interpretation. This identifier indicates a re-interpretation of the data in binary format, but does not change its value. For example:Int I; char * PTR = "Hello freind! "; I = reinterpret_cast <int> (PTR );This conversion method is rarely used.

 

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.