C ++ four types of conversion Operators

Source: Internet
Author: User

C ++ four types of conversion Operators
I. C-style conversion Operators


(type) expression




int firstNumber, secondNumber;double result = ((double)firstNumber)/secondNumber;


II. C ++ type conversion Operators

1. static_cast is basically as powerful as C-style type conversion and has the same meaning.

double result = static_cast

It also has functional limitations. For example, you cannot use static_cast to convert struct to int type or double type to pointer type like C type conversion. In addition, static_cast cannot remove the const attribute from the expression, because another new type conversion operator const_cast has this function.

2. const_cast is used to convert the const or volatileness attributes of an expression by type. By using const_cast, you emphasize to people and compilers that all you want to do through type conversion is to change the constness or volatileness attributes of some things.

Class Widget {...}; class SpecialWidget: public Widget {...}; void update (SpecialWidget * psw); SpecialWidget sw; // sw is a non-const object. Const SpecialWidget & csw = sw; // csw is a reference of sw. // It is a const object update (& csw); // error! You cannot pass a const SpecialWidget * variable // update (const_cast
(& Csw); // correct. The const of csw is converted explicitly (// The values of csw and sw can be updated in the update // function) update (SpecialWidget *) & csw); // same as above, but a C-style conversion Widget that is more difficult to recognize // * pw = new SpecialWidget; update (pw ); // error! The pw type is Widget *, but the // update function processes the update (const_cast) Type of SpecialWidget *.
(Pw); // error! Const_cast can only be used in places where constness or volatileness is affected ., // It cannot be used for type conversion to the inherited subclass.


Obviously, the most common purpose of const_cast is to remove the constants of an object.

3. dynamic_cast is used to safely perform type conversion down the class inheritance relationship. This means that you can use dynamic_cast to convert a pointer or reference to a base class to a pointer or reference to its derived class or its sibling class, and you can know whether the conversion is successful. If the conversion fails, a null pointer is returned (when type conversion is performed on the pointer) or an exception is thrown (when type conversion is performed on the reference)
Widget * pw;... update (dynamic_cast
(Pw); // correct. A pointer is passed to the update function. // It is a pointer to the pw of the variable type SpecialWidget. // if pw does point to an object, // otherwise, null pointers will be made when the previous one is passed. Void updateViaRef (SpecialWidget & rsw); updateViaRef (dynamic_cast
(* Pw); // correct. Pass the pw pointer to the updateViaRef function // SpecialWidget. If pw // does point to an object // otherwise, an exception is thrown.
Dynamic_casts is limited in helping you browse the hierarchy of inheritance. It cannot be used for types without virtual functions (see the M24 clause), nor can it be used to convert constness.
Int firstNumber, secondNumber; double result = dynamic_cast
(FirstNumber)/secondNumber; // error! Const SpecialWidget sw; update (dynamic_cast
(& Sw); // error! Dynamic_cast cannot be converted to const.
4. The conversion results of reinterpret_cast are almost all execution period definitions (implementation-defined. Therefore, it is difficult to port the code using reinterpret_casts. The most common purpose of reinterpret_casts is to convert between function pointer types.

For example, suppose you have an array of function pointers:

Typedef void (* FuncPtr) (); // FuncPtr is a pointer to a function. This function has no parameters. The return value type is voidFuncPtr funcPtrArray [10]. // funcPtrArray is an array that can hold 10 FuncPtrs pointers.
Let's assume that you want (for some inexplicable reason) to store a pointer to the following function into the funcPtrArray array: int doSomething ();
You can't do it without type conversion, because the doSomething function has an incorrect type for the funcPtrArray array. The function return value in the FuncPtrArray array is of the void type, while the doSomething function returns an int type.
FuncPtrArray [0] = & doSomething; // error! Type Mismatch
Reinterpret_cast allows you to force the compiler to look at them in your way:
funcPtrArray[0] = // this compiles reinterpret_cast

The code for converting function pointers cannot be transplanted (C ++ does not guarantee that all function pointers are represented in the same way ), in some cases, such conversions may produce incorrect results (see the M31 clause), so you should avoid converting the function pointer type.


Iii. Summary


The forced type conversion between static_cast and C is similar, except that struct cannot be converted to int or double to pointer type;

Const_cast is used to change the const and volatileness attributes. It is common to remove the const attributes (convert the const object to a non-const object );

Dynamic_cast is used for downward conversion of class inheritance relationships, but cannot be used for types without virtual functions;

The Conversion Result of reinterpret_cast is defined during the execution period.



Related Article

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.