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