C ++ type conversion summary and type conversion Summary

Source: Internet
Author: User

C ++ type conversion summary and type conversion Summary
The forced TYPE conversion of the C style is the most commonly used conversion method by programmers. All types of conversions are TYTE B = (TYPE). However, it is well known that the C ++ program has very strict type detection. If the C-style type conversion is used during the type conversion process, some problems may occur. So today I will summarize several types of conversion operators in C ++ style and their usage scenarios. C ++ supports four types of conversion operators: const_cast, static_cast, dynamic_cast, and reinterpret_cast.

 

Four types of conversion formats, such as: type B = static_cast <TYPE> (). A const_cast1. Remove the const or volatile attribute of the type. Struct SA {int I ;}; const SA ra; // ra. I = 10; // directly modify the const type. The compilation error SA & rb = const_cast <SA &> (ra); rb. I = 10; 2 static_cast is similar to a C-style forced conversion. Main application scenarios: 1. Conversion between the base class and the subclass: it is safe to convert the subclass pointer to the parent class pointer, but it is not safe to convert the parent class pointer to the subclass pointer. (Dynamic_cast is recommended for dynamic type conversion between the base class and the subclass) 2. Basic data type conversion. Enum, struct, int, char, float, etc. Static_cast cannot be used to convert irrelevant types (such as non-base classes and sub-classes. 3. convert a null pointer to a null pointer of the target type. 4. convert any expression type to void type. 5. static_cast cannot remove the const and volitale attributes of the type (use const_cast ). Int n = 6; // basic type conversion double d = static_cast <double> (n); int * pn = & n; // irrelevant type pointer conversion, compilation error double * d = static_cast <double *> (& n) // convert any type to void type void * p = static_cast <void *> (pn ); 3. dynamic_cast has conditional conversion and dynamic type conversion. during runtime, the type security check is performed (if the conversion fails, NULL is returned): 1. Secure conversion between the base class and the subclass. 2. virtual functions are required. 3. Cross conversion between different subclasses of the same base class. But the result is NULL. Class ItcastClass {public: int m_iNum; // The base class must have virtual functions. Dynamic_castvirtual void foo (){};};

 

Class HeiMaClass: public ItcastClass {public: char * m_szName [100]; void bar (){};};

 

ItcastClass * pb = new HeiMaClass (); // subclass-> parent class, static type conversion, correct, but not recommended HeiMaClass * pd1 = static_cast <HeiMaClass *> (pb ); // subclass-> parent class, dynamic type conversion, correct HeiMaClass * pd2 = dynamic_cast <HeiMaClass *> (pb); ItcastClass * PBS = new ItcastClass (); // parent class-> subclass, static type conversion, dangerous! The access subclass m_szName is a member out-of-the-box HeiMaClass * pd21 = static_cast <HeiMaClass *> (PBS); // The parent class-> subclass, which can be dynamically converted to a secure type. The result is NULLHeiMaClass * pd22 = dynamic_cast <HeiMaClass *> (master); 4 reinterpret_cast only re-interprets the type, but does not perform Binary Conversion: 1. The conversion type must be a pointer, reference, arithmetic type, function pointer, or member pointer. Bit-level conversion. It can convert a pointer to an integer, or an integer to a pointer (first, convert a pointer to an integer, and then convert the integer to the original type of pointer, you can also get the original pointer value ). However, you cannot convert a non-32bit instance to a pointer. N 2. The most common purpose is to convert between function pointer types. 3. It is difficult to ensure portability. Int doSomething () {return0 ;}; // FuncPtr is a pointer to a function. This function has no parameter and the return value type is voidtypedef void (* FuncPtr )(); // The array of 10 FuncPtrs pointers let us assume that you want (for some inexplicable reason) to store a pointer to the following function into the funcPtrArray array: FuncPtr funcPtrArray [10]; // compilation error! Type Mismatch. reinterpret_cast allows the compiler to view them in your way: funcPtrArrayfuncPtrArray [0] = & doSomething; // convert funcPtrArray [0] = reinterpret_cast <FuncPtr> (& doSomething) between different function pointer types. Finally, we will summarize the usage scenarios of the above four types of Conversion characters: 1. Use const_cast to remove the const attribute. 2. Use static_cast to convert the basic type. 3. daynamic_cast is used for type conversion between Polymorphism classes. 4. Use reinterpret_cast to convert different types of pointer types.

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.