Written questions record

Source: Internet
Author: User

To force type conversions:

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

Part 1th. Implicit type conversions

Also known as a "standard conversion", includes the following situations:
1) arithmetic conversion (arithmetic conversion): In a mixed-type arithmetic expression, the widest data type becomes the target conversion type.

2) One type expression is assigned to an object of another type: 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) Returns an expression from a function that is inconsistent with the return type: The target conversion type is the return type of the function

Part 2nd. An explicit type conversion

called Coercion type conversion (CAST)
C style: (Type-id)
C + + style: static_cast,dynamic_cast,reinterpret_cast, and const_cast .

There are four type conversions in standard C + +:static_cast,dynamic_cast,reinterpret_cast, and const_cast.

static_castusage: static_cast < Type-id > (expression)Description: This operator converts expression to the Type-id type, but does not have run-time type checking to guarantee the security of the conversion. dynamic_castusage: 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.

The dynamic_cast features type checking and is more secure than static_cast.

Reinpreter_castusage: 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). const_castusage: 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.

Written questions record

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.