Document directory
- Static_cast, dynamic_cast, reinterpret_cast, const_cast
Static_cast, dynamic_cast, reinterpret_cast, const_cast
Static_castUsage: static_cast <type-ID> (expression) This operator converts expression to type-ID, 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 the subclass to the base class representation). When performing downstream conversion (converting the base class pointer or reference to the subclass representation, because there is no dynamic type check, it is not safe. It is used to convert 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. Converts 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.
Dynamic_castUsage: 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. During downstream conversion, dynamic_cast has the type check function, which is safer than static_cast.
Class B {
Public:
Int m_inum;
Virtual void Foo ();
};
Class D: Public B {
Public:
Char * m_szname [100];
};
Void func (B * pb ){
D * pd1 = static_cast <D *> (PB );
D * Pd2 = dynamic_cast <D *> (PB );
}
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 any operations of the D-type on these two pointers; however, if Pb points to a B-type object, pd1 will be a pointer to this object, and operations of Type D on it will be insecure (for example, accessing 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 (the concept of virtual function table, in <inside C ++ object model>), only the 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.
Class {
Public:
Int m_inum;
Virtual void F (){}
};
Class B: Public {
};
Class D: Public {
};
Void Foo (){
B * pb = new B;
Pb-> m_inum = 100;
D * pd1 = static_cast <D *> (PB); // copile Error
D * Pd2 = dynamic_cast <D *> (PB); // Pd2 is null
Delete Pb;
}
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.
Reinpreter_castUsage: 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 (convert a pointer to an integer first, and then convert the integer to the original type, you can also get the original pointer value ). This operator is used in many ways.
Const_castUsage: const_cast <type_id> (expression) This operator is used to remove the const or volatile attributes of the type. In addition to const or volatile modification, type_id and expression are of the same type, and type_id must be a pointer or reference type. Constant pointers are converted to non-constant pointers and still point to the original objects. Constant references are converted to non-constant references and still point to the original objects; constant objects are converted to very large objects. Voiatile is similar to const. Take the following example:
Class B
{
Public:
B (): m_inum (0 ){};
~ B (){};
Public:
Int m_inum;
};
Void Foo ()
{
B;
// Pointer type
Const B * b1 = & B;
// B1-& gt; m_inum = 100; // compile Error
B * b2 = const_cast <B *> (B1 );
B2-> m_inum = 200;
Cout <B2-> m_inum <Endl;
// Reference type
Const B & B3 = B;
// B3.m _ inum = 100; // compile Error
B & B4 = const_cast <B &> (B3 );
B4.m _ inum = 300;
Cout <b4.m _ inum <Endl;
}
The above code will report an error during compilation. Because B1 is a pointer to a constant object, the object referred to cannot be modified. B3 is a constant object and cannot be changed; use const_cast to remove the constant attribute of the constant object referred to or referenced by it, and then you can change its data members at will. Note: B3 and B4 are two different objects.
For more details about type conversion, refer to: http://www.tenouk.com/download/pdf/Module22.pdf