Four conversion methods and four conversion methods in C ++

Source: Internet
Author: User

Four conversion methods and four conversion methods in C ++

There are c-style conversions, and c ++-style conversions. The conversion format of the c style is very simple (TYPE) EXPRESSION, but the TYPE conversion of the c style has many disadvantages, sometimes it is not suitable to use the c style conversion, because it can be converted between any types, for example, you can convert a pointer to a const object into a pointer to a non-const object, converting a pointer to a base-class object to a pointer to a derived-class object is a huge difference between the two conversions, but the traditional C-language type conversion is not differentiated. Another drawback is that the c-style conversion is not easy to find. It is composed of a bracket and an identifier, and such a thing is a lot in the c ++ 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 ))
Const int data; const_cast <int &> (data) = 1;


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.