Four types of coercion type conversions in C + +

Source: Internet
Author: User

Four types of coercion conversions: static_cast const_cast dynamic_cast reinterpret_cast

1, static_cast

Any type conversions that the compiler implicitly executes can be done by the static_cast display.

① is used for the conversion of pointers or references between base and subclass classes in a class hierarchy. It is safe to make an upstream conversion (a pointer to a class or a reference to a base class representation), and a downstream conversion (to convert a base class pointer or reference to a subclass representation) is unsafe because there is no dynamic type checking. ② is used for conversions between basic data types, such as converting int to char, and converting int to enum. The security of this conversion is also to be ensured by the developer. ③ converts a null pointer to a null pointer of the target type. ④ converts any type of expression to a void type.

2. Const_cast: The Const property of the expression will be converted.

The constant pointer is converted into a very pointer, and still points to the original object;

A constant reference is converted to a very literal reference and still points to the original object;

A constant object is converted to a very mass object.

3. dynamic_cast: Converts a reference or pointer of a base class type object to another type of reference or pointer in the same inheritance hierarchy. The pointer used with dynamic_cast must be valid-must be 0 or point to an object.

The dynamic_cast operator performs two operations at a time, first verifying that the requested conversion is valid, that only the conversion is valid, and that the operation characters the actual conversion.

(1) The other three kinds are completed at compile time, dynamic_cast is run-time processing, the runtime to do type checking.

(2) cannot be used for casting of built-in base data types.

(3) dynamic_cast Conversion If successful, the pointer or reference to the class is returned, and NULL is returned if the conversion fails.

(4) using dynamic_cast to convert, there must be a virtual function in the base class, otherwise the compilation does not pass.

The reason why a virtual function needs to be detected in B is that there is a virtual function in the class, which means that it has a situation where a base class pointer or reference is intended to point to a derived class object, at which point the transformation makes sense.

This is because runtime type checking requires run-time type information, and this information is stored in the virtual function table of the class, and only the class that defines the virtual function has a virtual function table.

(5) When converting classes, dynamic_cast and static_cast have the same effect when upstream conversions are performed between class hierarchies. In the downstream conversion, dynamic_cast has the function of type checking, which is more secure than static_cast. The up conversion is the downward transition to the subclass object, and the parent pointer is converted to the child class pointer. The success of a down conversion is also related to the type to be converted, that is, the actual type of the object to be converted to is necessarily the same as the object type after the conversion, or the conversion fails.

4, Reinterpret_cast:

Usage:reinterpret_cast<type-id> (expression)

Type-id must be a pointer, reference, arithmetic type, function pointer, or member pointer.

It can convert a pointer to an integer, or an integer to a pointer (a pointer is converted to an integer, the integer is converted to the original type of pointer, and the original pointer value can be obtained).

Summary: Go to the const attribute with const_cast. Basic type conversion with static_cast. A type conversion between polymorphic classes is used with Daynamic_cast. Different types of pointer-type conversions with Reinterpreter_cast.

Four types of coercion type conversions in C + +

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.