We all know that no matter what programming language we useForced type Conversion FunctionEach function can forcibly convert an expression to a specific data type. The following describesC ++Forced type conversion function in.
There are four types of forced conversion operators in Standard c ++:
- Const_cast,
- Reinterpret_cast,
- Static_cast,
- Dynamic_cast and so on.
1) static_cast <T *>)
Converts address a to type T. T and a must be pointer, reference, arithmetic, or enumeration.
Expression static_cast <T *> a). The value of a is converted to the type T specified in the template. During the running conversion process, no type check is performed to ensure the security of the conversion.
Example:
- Class B {...};
- Class D: public B {...};
- Void f (B * pb, D * pd)
- {
- D * pd2 = static_cast <D *> (pb); // insecure. pb may only be the pointer of B * PBS = static_cast <B *> (pd ); // safe
- ...
- }
- Class B {...};
- Class D: public B {...};
- Void f (B * pb, D * pd)
- {
- D * pd2 = static_cast <D *> (pb); // insecure. pb may be a pointer to B.
- B * PBS = static_cast <B *> (pd); // safe
- ...
- }
- Class B {...};
- Class D: public B {...};
- Void f (B * pb, D * pd)
- {
- D * pd2 = static_cast <D *> (pb); // insecure. pb may be a pointer to B.
- B * PBS = static_cast <B *> (pd); // safe
- ...
- }
2) dynamic_cast <T *>)
Completes the upgrade of the class hierarchy. T must be a pointer, reference, or non-type pointer. A must be a pointer or referenced expression.
Expression dynamic_cast <T *> a) converts a value to an object pointer of type T. If type T is not a base type of a, this operation returns a null pointer.
Example:
- Class {...};
- Class B {...};
- Void f ()
- {
- A * pa = new;
- B * pb = new B;
- Void * pv = dynamic_cast <A *> (pa );
- // Pv now points to an object of type
- ...
- Pv = dynamic_cast <B *> (pb );
- // Pv now points to an object of type B
- }
3) const_cast <T *>)
Remove constants from the type. Except for const or the number of unstable addresses, T and a must be of the same type.
Expression const_cast <T *> a) is used to remove the following attributes from a class: const, volatile, and _ unaligned.
Example:
- Class {...};
- Void f ()
- {
- Const A * pa = new A; // const object
- A * pb; // non-const object
- // Pb = pa; // an error occurs. You cannot assign a const object pointer to a non-const object.
- Pb = const_cast <A *> (pa); // now OK
- ...
- }
- Class {...};
- Void f ()
- {
- Const A * pa = new A; // const object
- A * pb; // non-const object
- // Pb = pa; // an error occurs. You cannot assign a const object pointer to a non-const object.
- Pb = const_cast <A *> (pa); // now OK
- ...
- }
- Class {...};
- Void f ()
- {
- Const A * pa = new A; // const object
- A * pb; // non-const object
- // Pb = pa; // an error occurs. You cannot assign a const object pointer to a non-const object.
- Pb = const_cast <A *> (pa); // now OK
- ...
- }
4) reinterpret_cast <T *>)
Any pointer can be converted to another type of pointer. T must be a pointer, reference, arithmetic type, pointer to a function, or pointer to a class member.
Expression reinterpret_cast <T *> a) can be used for conversion from char * to int *, or from One_class * To Unrelated_class *, which may be insecure.
Example:
- Class {...};
- Class B {...};
- Void f ()
- {
- A * pa = new;
- Void * pv = reinterpret_cast <A *> (pa );
- // Pv now points to an object of type B, which may be insecure
- ...
- }
However, there are also some weak types in C ++, so do not use forced type conversion. I hope this article will help you.