Use the Standard C ++ type conversion characters static_cast, dynamic_cast, reinterpret_cast, and const_cast.
3.1 static_cast
Usage: static_cast <type-ID> (expression)
This operator converts expression to the Type-ID type, but does not check the runtime type 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_cast cannot convert the const, volitale, or _ unaligned attribute of expression.
3.2 dynamic_cast
Usage: dynamic_cast <type-ID> (expression)
This operator converts expression to type-ID objects. Type-ID must be a class pointer, class reference, or void *;
If type-ID is a class pointer type, expression must also be a pointer. If type-ID is a reference, expression 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.
When performing upstream conversion between classes, dynamic_cast and static_cast have the same effect;
Dynamic_cast provides the type check function for downstream conversions, which is safer than static_cast.
1 class B {
2 public:
3 int m_inum;
4 virtual void Foo ();
5 };
6
7 Class D: Public B {
8 public:
9 char * m_szname [100];
10 };
11
12 Void func (B * pb ){
13 D * pd1 = static_cast <D *> (PB );
14 D * Pd2 = dynamic_cast <D *> (PB );
15}
In the above Code segment, if PB points to a D-type object, pd1 and Pd2 are the same, and it is safe to execute D-type operations on these two pointers;
However, if PB points to a B-type object, pd1 will be a pointer to this object, it is not safe to perform operations of the D type (for example, to access m_szname ),
Pd2 is a null pointer.
Note: B must have virtual functions; otherwise, compilation errors may occur. static_cast does not have this restriction.
This is because the runtime type check requires runtime type information, which is stored in the virtual function table of the class (
For more information about the concept of virtual function tables, see <inside C ++ object model>). Only classes that define virtual functions have virtual function tables,
Classes that do not define virtual functions do not have virtual function tables.
In addition, dynamic_cast also supports cross cast ). The following code is used.
1 class {
2 public:
3 int m_inum;
4 virtual void F (){}
5 };
6
7 Class B: Public {
8 };
9
10 Class D: Public {
11 };
12
13 void Foo (){
14 B * pb = new B;
15 Pb-> m_inum = 100;
16
17 d * pd1 = static_cast <D *> (PB); // compile Error
18 D * Pd2 = dynamic_cast <D *> (PB); // Pd2 is null
19 Delete Pb;
20}
In function Foo, using static_cast for conversion is not allowed, and errors will occur during compilation. Using dynamic_cast for conversion is allowed, and the result is a null pointer.
3.3 reinpreter_cast
Usage: reinpreter_cast <type-ID> (expression)
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.
3.4 const_cast
Usage: const_cast <type_id> (expression)
This operator is used to modify the const or volatile attributes of the type. In addition to const or volatile modification, type_id and expression are of the same type.
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. Take the following example: 1 class B {
2 public:
3 int m_inum;
4}
5 void Foo (){
6 const B B1;
7 b1.m _ inum = 100; // comile Error
8 B b2 = const_cast <B> (B1 );
9 B2. m_inum = 200; // fine
10}
The above code will report an error during compilation, because B1 is a constant object and cannot be changed;
Using const_cast to convert it into a constant object, you can change its data members at will. Note: B1 and B2 are two different objects.
From: http://www.cppblog.com/dawnbreak/articles/68122.html