Forced type conversion in C ++

Source: Internet
Author: User

There are four forced conversions in C ++: const_cast, static_cast, dynamic_cast, and reinterpret_cast, which are described as follows:

 

1. const_cast:

FormConst_cast <type> (expression) is used to modify the const or volatile attributes of a type. Except for const or volatile modification, the type is the same as the expression type;

It is mainly used to remove const:

Convert (const) pointer-to-const to (const) pointer-to-nonconst;

Convert referece-to-const to reference-to-nonconst;

You cannot convert a const object to a nonconst object,

If not, const I = 1; Int J = const_cast <int> (I );

But it can be directly: Int J = I;

2. static_cast:

FormStatic_cast <type> (expression) to convert expression to type.

There is no runtime type check to ensure the security of the conversion.

It is mainly used:

Conversion between basic data types. For example, convert int to Char and convert int to enum. Note: static_cast <char> (intvar) and static <char &> (intvar) difference: the former creates a temporary variable, and the latter creates a reference, which is bound to the current object intvar;

Converts non-const to a const object. Sometimes, you need to explicitly convert nonconst to a const object to call functions or member functions that are overloaded due to const, for example, to call a const member function in a non-const member function, you must: static <const someclass &> (* This ). constfunction (...);

In the class hierarchy, objects, pointers, or references between classes and subclasses are converted. When downloading conversions (converting base class objects, pointers, or references to subclass representations, because there is no dynamic type check, it is not safe.

Converts any type of NULL pointer to another type;

Convert any type of pointer to the void type or vice versa.

3. dynamic_cast:

FormDynamic_cast <type> (expression) to convert expression to type.

Dynamic_cast performs a dynamic check at runtime, Which is safer than static_cast.

Used:

The pointer or reference transformation between the base class and the derived class in the inheritance relationship. In particular, you can use dynamic_cast to convert the pointer or reference to the base class to a pointer or reference to its derived class or its sibling class; no inheritance relationship exists, and pointers and references can be converted as long as the pointer or referenced object has at least one virtual function, such: you can convert any pointer to a virtual function object to void *, const void *, or volatile void *, the result is that the generated Pointer Points to the beginning of "the original Pointer Points to the object memory.

If the type of the object actually pointed to or referenced by expression is indeed type, the operation succeeds; otherwise, the operation fails. If the conversion fails, a null pointer is returned (when the type conversion is performed on the pointer) or throw an exception (when converting the type of the reference ).

4. reinterpret_cast:

FormReinpreter_cast <type> (expression) to convert expression to type. It is mainly used to convert a type pointer to another type pointer, such as a function pointer.

Static check during compilation, which is generally not portable, should be used with caution.

Used to convert one type of pointer or reference to another type of pointer or reference. to convert a pointer to an integer type, or convert an integer to a pointer (first, convert a pointer to an integer. You can also obtain the original pointer value after converting the integer to the original type ).

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.