Haha, this title is a little funny! Smile, 10 years less, hope everyone hi heart!
There are four types of forced-type conversions in C + +: Static_cast,reinterpret_cast,const_cast,dynamic_cast.
1) static_cast<t*> (a)
Converting address A to type t,t and a must be pointers, references, base data types, or enumeration types. During the run-time conversion process, no type checking is performed to ensure the security of the conversion.
class B {...}; class Public B {...}; void F (b* pb, d* PD) { D* pd2 = static_cast<d*> (pb); // not safe, PB may just be a pointer to B b* PB2 = static_cast<b*> (PD); // Safe for ...}
2) dynamic_cast<t*> (a)
To complete the promotion in the class hierarchy, T must be a pointer, reference, or untyped pointer. A must be an expression that determines a pointer or reference.
The expression dynamic_cast<t*> (a) converts a value to an object pointer of type T. If the type T is not a base type of a, the operation returns a null pointer.
class A {...}; class B {...}; void f () { anew A; bnew b; void* PV = dynamic_cast<a*>(PA); // PV now points to an object of type a ... = Dynamic_cast<b*>(pb); // PV now points to an object of type B }
3) const_cast<t*> (a)
Remove the constants in the type, except for const or unstable variable addresses, and T and a must be of the same type.
The expression const_cast<t*> (a) is used to remove the following properties from a class: const, volatile, and _unaligned.
class A {...}; void f () {constnew A; // Const Object *PB; // Non-const objects // // There will be an error, you cannot assign a const object pointer to a non-const object // now OK ...}
4) reinterpret_cast<t*> (a)
Any pointer can be converted to another type of pointer, t must be a pointer, a reference, an arithmetic type, a pointer to a function, or a pointer to a class member.
The expression reinterpret_cast<t*> (a) can be used to convert char* to int*, or one_class* to unrelated_class*, and so on, so it is unsafe.
class A {...}; class B {...}; void f () { anew A; void* PV = reinterpret_cast<a*>(PA); // PV now points to an object of type A, which may be unsafe ...}
The four kings of "forced conversion" in C + +