Application differs static_cast, const_cast, reinterpret_cast, dynamic_cast of

(1)static_cast:

        It is a major conversion type used between the base class and type conversion. But they have differences, specifically to see the following example:

Basic type conversion:

        We all know that when the basic data type conversion, high-low type of data transfer data type is actually implicit conversion, is safe, why? Because there is no loss of accuracy, such as float a = 1.234, converted int b = a ;, for completely ok integer int b, because there is no loss, but when the low conversion type data into high precision type data loss appeared , such as int c = 1, when converted into float type d, the decimal point is lost, it is necessary at this time, we often say display type conversion to be cast, c = (float) d, where may be with static_cast, as are specific static data, the same effect.

Class type:

       And a little bit different basic types here, simply, is the inheritance, the parent type subtype turn can be converted by static_cast, but the parent type by static_cast does not. But it can be converted, through another embodiment of dynamic_cast, see the following concrete (4) Introduction.

          

(2)const_cast:

      It is a relatively safe for performing, based on the contents of data type conversion, its role is the general attribute data types const removed, so that the normally pointer types to normal pointer, reference will often convert into common reference .

eg:

    void func( const int *p)

   {

           int * q = const_cast<int *>p;

           (*q)++;

   }

Description: Under normal circumstances, const int * p demand value stored at the address p changes can not occur, often represents a pointer, but the const const_cast herein by nature removed, resulting in the corresponding address value becomes a normal pointer value, which can be modified. Often cited convert regular references also do so.

(3)reinterpret_cast:

         It is used to convert between any type, having a great risk and uncertainty, it can be converted to each other between any pointer, the pointer even if the content is unrelated. So this should be used in the program is to be avoided.

(4)dynamic_cast:

         It is mainly used to convert between classes. Often said uplink and downlink converting conversion between the class hierarchy, may also be used to convert between cross class. When switching between uplink class hierarchy, and static_cast dynamic_cast effect is the same; during downconversion, dynamic_cast has a function of checking the type of safer than static_cast.

eg1:

      class A

      {};

      clsss B : public A

      {};

      B b;

      A *a =dynamic_cast<B*>(&b); 

 

eg2:

        class A

        {};

        class B :public A

        {};

         A *a =new B ;

         A *b =new A;

         B *c = dynamic_cast<B>a;        (TRUE)

         B *d =dynamic_castt<B>b;        (FALSE)

 

Published 23 original articles · won praise 4 · Views 9980

Guess you like

Origin blog.csdn.net/hxp1994/article/details/89861886