C + + type conversions

Source: Internet
Author: User

C + + type conversions are divided into: implicit type conversions and explicit type conversions

Implicit conversions

There are several situations:

1. Arithmetic conversion (arithmetic conversion): In a mixed-type arithmetic expression, the widest data type becomes the target conversion type.

2, one type of expression is assigned to another type of object: The target type is the type of the object being assigned

3. Passing an expression as an argument to a function call when the formal parameter and argument types are inconsistent: the target conversion type is the type of the parameter

4, return an expression from a function, the expression type is inconsistent with the return type: The target conversion type is the return type of the function


Show transformations

called Coercion type conversion (CAST)

C-style: (Type-id) using parentheses

C + + style: static_cast, dynamic_cast, reinterpret_cast, and Const_cast four


1, Const_cast

Usage:const_cast<type_id> (expression)

Description: This operator is used to modify the const or volatile properties of a type. In addition to const or volatile adornments, the type_id and expression types are the same.

Attention:

you must use pointers or references, It doesn't really change the const property of the original class type (or base type), it just provides an interface (pointer or reference) that can

Use this interface to change the type value.


2,static_cast

Usage: static_cast < Type-id > (expression)

Description: Any type conversion implicitly directed by the compiler can be done explicitly by static_cast


3, Reinpreter_cast

Usage:reinpreter_cast<type-id> (expression)
Description: 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).


4, dynamic_cast

Usage: dynamic_cast < Type-id > (expression)

Description: This operator converts expression to an object of type Type-id. Type-id must be a pointer to a class, a reference to a class, or void *; if Type-id is a class pointer type, expression must also be a pointer, and if Type-id is a reference, expression must also be a reference.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + type conversions

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.