1. Implicit type conversion (standard conversion)
1. Arithmetic conversion
In mixed-type expressions, the widest data type is the target conversion type, such as:
int a = 3; double b = 1.12; //Expression a+b a is converted to double type
2. Assignment conversion
Direct assignment
An expression of one type is assigned to another type of object: it will be converted to the type of the object being assigned, such as:
int a = 3; double b = 1.12; a = b;//bConvert from double to int type
Function parameter assignment
When passing an expression or a variable as an actual parameter to a function call, the actual parameter formal parameter types are inconsistent and will be converted to a formal parameter type, such as:
int fun(int a,int b) { return a + b; } int main() { double a = 3.14; double b = 1.12; cout << fun(a, b) << endl;//Convert double type to int return 0; }
Function return value assignment
Return an expression from a function. When the expression type is inconsistent with the return type, the target conversion type is the return type of the function, such as:
int fun(double a,double b) { return a + b;//The incoming parameter is double, the return value is int, convert the double type to int} int main() { double a = 3.14; double b = 1.12; cout << fun(a, b) << endl;//Convert double type to int return 0; }
Note: When the void pointer is assigned to other pointers of the specified type, there is no standard conversion, and a compilation error occurs.
void* p = 0;//0 Convert from int type to void * typeint* b = p;//Report an error
2. Display type conversion (cast type conversion)
Common language style: (type-id)A // Convert A to type-id type
C++ language style: standard C++ four-category type conversion (static_cast, dynamic_cast, reinterpret_cast, const_cast)
1、static_cast
usage:
static_cast<type_id>(expression)
illustrate
Convert expression to type_id type, but no runtime type checking to ensure the security of the conversion
Why do I need static_cast cast?
- void pointer->Other type of pointer
- Change the usual standard conversion
- Avoid possible multiple conversion ambiguity
Common usage
- Used for conversion of pointers or references between base classes and subclasses in a class hierarchy. It is safe to perform uplink conversion (convert a subclass pointer or reference to a base class representation); it is unsafe when performing downlink conversion (convert a base class pointer or reference to a subclass pointer or reference) because there is no dynamic type check.
- Used for conversion between basic data types, such as converting int to char and int to enum. The security of this conversion is also guaranteed by the developer.
- Convert void pointer to target type pointer (unsafe!!)
- Any type of expression is converted to void type.
Note: static_cast cannot convert the const, voltage, or __unaligned attributes of expression.
2、dynamic_cast
usage
dynamic_cast<type_id>(expression)
illustrate
This operator converts expression to an object of type-id. Type-id must be a pointer to the class, a reference to the class, or void *; if type-id is a class pointer type, then expression must also be a pointer, and if type-id is a reference, then expression must also be a reference.
Main uses
- When virtual functions cannot be used, you can use dynamic_cast to solve the problem
- It is mainly used for uplink and downlink conversion between class levels, and can also be used for cross-conversion between classes. When performing uplink conversion, it is the same as the effect of static_cast. When performing downlink conversion, dynamic_cast has type checking function more safe than static_cast, such as:
class A { public: int name; virtual void fun(); }; class B :public A { public: char* namesize[100]; }; void foo(A *p) { B* p1 = static_cast<B*>(p); B* p2 = dynamic_cast<B*>(p); }
Explain the above code: If p actually points to an object of class B, then p1 and p2 are the same, and it is safe to perform any operation of type B on these two pointers. If P points to a Class A object, then p1 will be a pointer to that object, and it will be unsafe to perform type B operations on it (such as accessing namesize), while p2 will be a null pointer (i.e. 0, because dynamic_cast fails). Also note: A must have virtual functions, otherwise there will be an error in compilation, and static_cast does not have this limitation. This is because runtime type check requires runtime type information, and this information is stored in the virtual function table of the class. Only classes that define virtual functions have virtual function tables.
Cross-conversion, such as:
class A { public: int name; virtual void fun(); }; class B1 :public A { }; class B2 :public A { }; void foo() { B1* p1 = new B1; p1->name = 100; B2* p2 = static_cast<B2*>(p1);//Compilation error occurred B2* p2 = dynamic_cast<B2*>(p1);//p2 is a null pointer}
3、reinpreter_cast
usage
reinpreter_cast<type-id>(expression)
illustrate
type-id must be a pointer, reference, arithmetic type, function pointer, or member pointer. It can convert a pointer into an integer, or an integer into a pointer (first convert a pointer into an integer, then convert the integer into a pointer of the original type, and you can also get the original pointer value).
4、const_cast
usage
const_cast<type_id> (expression)
illustrate
This operator is used to modify the type's const or volatile attributes. Except for const or volatile modifications, type_id and expression are the same.
The constant pointer is converted into a non-const pointer and still points to the original object; the constant reference is converted into a non-const reference and still points to the original object; the constant object is converted into a non-const object.
class A { public: int name; }; void foo() { const A a1; = 100;//Compilation error occurred A a2 = const_cast<A>(a1); = 100; }
The above code will report an error when compiling because a1 is a constant object and cannot be changed; if you use const_cast to convert it into a constant object, you can change its data members at will. Note: a1 and a2 are two different objects.
This is the end of this article about the detailed explanation of the conversion between different data types in C/C++. For more related C/C++ data type conversion content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!