A, C language of the forced conversion
1.1 Implicit conversions
Assignment and operation between different data types, function call passing parameters, etc., done by the compiler
int ntmp = 10;
Short sTmp = ntmp; At this point you need to pay attention to whether the data overflows during the conversion (large byte to small section)
1.2 Dominant conversions
Precede the variable with a (Type) for a dominant conversion, such as:
Class Test;
Test *test;
t* obj = (t*) test;//casts the object to T
Ii. Conversion of C + + language
The enforcement of the C + + language consists of four standard conversion characters, static_cast, Const_cast, dynamic_cast, reinterpret_cast, except for the first four turns
In C + +, which inherits the conversion of the language, but this conversion is not secure and strict.
2.1 static_cast
Usage: static_cast < typeid > (exdivssion)
The operator converts exdivssion to typeid type, However, there is no run-time type check to guarantee the security of the conversion. It has several uses:
① is used for pointers or references between base and subclass classes in a class hierarchy. The
makes an upstream conversion (a pointer to a class or a reference to a base class representation) is safe, and
a downstream conversion (a base class pointer or reference to a subclass representation) is unsafe because there is no dynamic type checking. The
② 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. The
③ converts a null pointer to a null pointer of the target type. The
④ converts an expression of any type to a void type.
Note: static_cast cannot convert Exdivssion's const, Volitale, or __unaligned properties.
2.2dynamic_cast
Usage: dynamic_cast < typeid > (exdivssion)
The operator converts exdivssion to an object of type typeID. Type-id must be a pointer to a class, a reference to a class, or void *;
① If Type-id is a class pointer type, then exdivssion must also be a pointer, and if Type-id is a reference, Exdivssion also
must be a reference. The
②dynamic_cast is primarily used for upstream and downstream conversions between class hierarchies, and can also be used for cross-conversion between classes.
The effect of dynamic_cast and static_cast is the same when upstream conversions are made between class hierarchies;
③ the ability to type check when making a downstream conversion, than the static _cast more secure
class CBase
{
Public:
virtual void Printfout () {};
};
Class Test:public CBase
{
Private:
int m_ncount;
};
void func (CBase *pb)
{
Test *pd1 = static_cast (pb);
Test *pd2 = dynamic_cast (PB);
}
Note: Normally dynamic_cast, it makes sense to have a base class turned into a class, but if there is no virtual function, the base class is converted to a subclass with an error,
This is a virtual function for dynamic_cast, and the virtual function table in the C + + object model, The object instance is preceded by a virtual function table pointer.
2.3 Reindivter_cast
Usage:reindivter_cast<typeid> (exdivssion)
Type-id must be a pointer, reference, arithmetic type, function pointer, or member pointer.
It can convert a pointer to an integer, or convert an integer to a pointer (a pointer is converted to an integer first,
The original pointer value can also be obtained when the integer is converted to a pointer of the original type.
2.4 const_cast
Usage:const_cast<typeid> (exdivssion)
The operator is used to modify the const or volatile properties of a type. In addition to const or volatile adornments, the types of typeid and exdivssion are the same.
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, and the constant object is converted to a very mass object.
const int tmpvalue = 10;
Const int* Nconst = &tmpValue;
int* Modify = C
Casts in C + +