In the C language, if you want to convert the type conversion, use the method of forced type conversion.
(T) Expression
However, the type conversion characters static_cast, dynamic_cast, reinterpret_cast, and const_cast are provided in C ++. The following is an example.
Static_cast
Usage:Static_cast<Type-ID> (exdivssion)
This operator converts exdivssion to the Type-ID type, but there is no runtime type check to ensure the conversion security. It has the following usage:
① It is used to convert pointers or references between classes and subclasses in the class hierarchy.
It is safe to perform upstream conversion (converting the pointer or reference of a subclass to a base class;
When performing a downstream conversion (converting a base class pointer or reference to a subclass), it is not safe because there is no dynamic type check.
② It is used for conversion between basic data types. For example, convert int to Char and convert int to enum. The security of such conversions must also be ensured by developers.
③ Convert a null pointer to a null pointer of the target type.
④ Convert any type of expression to void type.
Note:Static_castThe const, volitale, or _ unaligned attribute of exdivssion cannot be converted.
# Include <iostream> <br/> using namespace STD; </P> <p> Class A <br/>{< br/> public: <br/> A (int I): A (I) <br/>{}< br/> A () <br/>{}< br/> int; <br/>}; <br/> Class B: public a <br/>{< br/> Public: <br/> int B; <br/> }; <br/> void main () <br/> {<br/> // static_cast <br/> A * m = new A (10 ); <br/> B * n = static_cast <B *> (m); // converts the base class pointer to a subclass pointer, this is not safe <br/> N-> A = 100; <br/> cout <m-> A <Endl; <br/> Delete m; <br/> N = new B; <br/> N-> B = 100; <br/> N-> A = 100; <br/> M = static_cast <A *> (n); // converts the subclass pointer to a parent class pointer. <br/> M-> A = 1000; <br/> cout <n-> A <Endl; <br/> cout <n-> B <Endl; <br/> Delete N; <br/>}
The running result is
100
1000
100
Dynamic_cast
Usage: dynamic_cast <type-ID> (exdivssion)
This operator converts exdivssion to an object of the Type-ID type. Type-ID must be a class pointer, class reference, or void *;
If type-ID is a class pointer type, exdivssion must also be a pointer. If type-ID is a reference, exdivssion must also be a reference.
Dynamic_cast is mainly used for upstream and downstream conversions between classes, and can also be used for cross conversions between classes.
Dynamic_cast andStatic_castThe results are the same;
When performing a downstream conversion, dynamic_cast has the type check function.Static_castMore secure.
# Include <iostream> <br/> using namespace STD; </P> <p> Class A <br/>{< br/> public: <br/> A (int I): A (I) <br/>{}< br/> () <br/>{}< br/> virtual void fun () {}< br/> int A; <br/>}; <br/> Class B: public A <br/>{< br/> Public: <br/> int B; <br/>}; <br/> void main () <br/> {<br/> // dynamic_cast <br/> A * m = new A (10 ); <br/> B * n = dynamic_cast <B *> (m); // downward conversion. The base class pointer is converted to a subclass pointer <br/> N-> A = 100; <br/> cout <m-> A <Endl; <br/> Delete m; <br/> N = new B; <br/> N-> B = 100; <br/> N-> A = 100; <br/> M = dynamic_cast <A *> (N ); <br/> M-> A = 1000; <br/> cout <n-> A <Endl; <br/> cout <n-> B <Endl; <br/> Delete N; <br/>}
Note that a virtual function is added to Class A. Otherwise, compilation errors may occur, saying that Class A does not have a virtual function table, but static_cast does not. This is the difference between them,
This is because the runtime type check requires the runtime type information, which is stored in the virtual function table of the class. Only the class that defines the virtual function has the virtual function table, classes that do not define virtual functions do not have virtual function tables.
In addition, an error occurred while running the program and exited unexpectedly. This is because the type is checked during the downstream conversion. At this time, when the M pointer is converted to N pointer, the N pointer is null, therefore, an exception occurred when accessing the member variable in Class.
In addition, dynamic_cast can perform cross conversion. For more information, see the example below.
# Include <iostream> <br/> using namespace STD; </P> <p> Class A <br/>{< br/> public: <br/> A (int I): A (I) <br/>{}< br/> () <br/>{}< br/> virtual void fun () {}< br/> int A; <br/>}; <br/> Class B: public A <br/>{< br/> Public: <br/> int B; <br/>}; <br/> Class C: public A <br/>{< br/> Public: <br/> int B; <br/>}; <br/> void main () <br/> {<br/> B * n = new B; <br/> C * k; <br/> K = dynamic_cast <C *> (N ); <br/> Delete N; <br/>}
If static_cast is used, a compilation error occurs, but dynamic_cast is acceptable. The result K pointer is a null pointer.
Reinterpret
Reinterpret_cast (exdivssion)
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 (first, convert a pointer to an integer,
You can also obtain the original pointer value after converting the integer to the original type ).
This operator is used in many ways.
See the following example.
# Include <iostream> <br/> using namespace STD; <br/> void main () <br/>{< br/> int * B = new int [100]; <br/> cout <B <Endl; <br/> char * c = new char [11]; <br/> strcpy (C, "ecjtuseany "); <br/> cout <C <Endl; <br/> cout <reinterpret_cast <int> (c) <Endl; <br/> Delete B; <br/> Delete C; <br/>}
The running result is as follows:
Const_cast
Usage: const_cast (exdivssion)
This operator is used to modify the const or volatile attributes of the type. Apart from the const or volatile modifier, The type_id and exdivssion types are the same.
Constant pointers are converted to non-constant pointers and still point to the original object;
Constant reference is converted to a non-constant reference and still points to the original object. Constant object is converted to a non-constant object.
Voiatile and const classes.
# Include <iostream> <br/> using namespace STD; </P> <p> Class A <br/>{< br/> public: <br/> A (int I): A (I) <br/>{}< br/> () <br/>{}< br/> virtual void fun () {}< br/> int A; <br/>}; <br/> Class B: public A <br/>{< br/> Public: <br/> int B; <br/>}; <br/> void main () <br/>{< br/> const A * m = new A (10); <br/> A * n = const_cast <A *> (m ); <br/> N-> A = 100; <br/> cout <n-> A <Endl; <br/> Delete m; <br/>}
The result is 100. m points to the object that has been modified.