Summary of of based on c++ casts
- 2020-04-01 21:38:36
- OfStack
What is a type conversion?
Type casting means changing the representation of a variable by changing its type to something else. In order to cast a simple object into another object you would use the traditional cast operator.
C and C++ type conversion
C:
(T)element or T ( element )
C + + :
reinterpret_cast<T*> (expression)
dynamic_cast<T*> (expression)
static_cast<T*> (expression)
const_cast<T*> (expression)
Each of the four forced transition forms of C++ is suitable for a specific purpose:
, dynamic_cast is mainly used to perform "safe downcasting", that is, to determine whether an object is a specific type in an inheritance system. It is the only forced transformation that cannot be performed with the old style syntax, and the only forced transformation that can have significant runtime costs.
・ static_cast can be used for forcing implicit type conversion (for example, non - const object into a const object, int transformation into a double, and so on), it can also be used for many such conversion reverse transformation (for example, void * pointer to pointer type, a base class pointer to a derived class pointer), but it can't be a const object into a non - const object (const_cast can only), it is the most close to the C - style transformation.
, const_cast is generally used to force the object to be constant. It is the only C++ style forced transformation that can do this.
, reinterpret_cast is intended to be the result of an underlying forced transformation that results in implementation-dependent (that is, non-portable), for example, converting a pointer to an integer. Such forced transitions should be extremely rare outside of the underlying code.
Popular explanation:
dynamic_cast Typically used when converting between base and derived classes static_cast The general transformation, if you don't know which one to use, use this const_cast Mainly for const and volatile conversion reinterpret_cast Used to convert between no associations, such as a character pointer to an integer
Specific analysis:
1) static_cast < T * > (a) The compiler processes at compile time
Converts address a to type T, and T and a must be Pointers, references, arithmetic types, or enumeration types. Expression static_cast < T * > (a), the value of a is converted to the type T specified in the template. During run-time conversions, type checking is not performed to ensure the security of the conversions. Static_cast converts between built-in data types, and for classes, only between associated pointer types. Pointers can be converted to and from within the inheritance system, but not to a type outside the inheritance system
class A { ... };
class B { ... };
class D : public B { ... };
void f(B* pb, D* pd)
{
D* pd2 = static_cast<D*>(pb); //No, pb might just be a pointer to B
B* pb2 = static_cast<B*>(pd); //The safety of
A* pa2 = static_cast<A*>(pb); //Error A has no inheritance relationship with B
...
}
2) dynamic_cast < T * > (a) At run time, the transformation is checked to see if it is possible
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. Dynamic_cast can only be applied to Pointers or references and does not support built-in data types 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. Not only does it check, like static_cast, whether the two Pointers before and after the transformation belong to the same inheritance tree, it also checks the actual type of the object referenced by the pointer to see if the transformation is feasible. If it can, it returns a new pointer and even calculates the necessary offset to handle multiple inheritance. If there is no conversion between the two Pointers, the conversion fails and a NULL pointer is returned. Obviously, in order for dynamic_cast to work properly, the compiler must support run-time type information (RTTI).
class Base { virtual dummy() {} };
class Derived : public Base {};
Base* b1 = new Derived;
Base* b2 = new Base;
Derived* d1 = dynamic_cast<Derived *>(b1); // succeeds
Derived* d2 = dynamic_cast<Derived *>(b2); // fails: returns 'NULL'
3) const_cast < T * > (a) the compiler processes at compile time
Remove constants from a type, and T and a must be of the same type, except for const or unstable indexed Numbers. Expression const_cast < T * > (a) is used to remove the following attributes from a class: const, volatile, and arbitration. For the type itself defined as const, even if you remove const, you should be careful when you manipulate this piece of content, only r can not w operation, otherwise you will still error The const_cast operation cannot be converted between different classes. Instead, it simply converts an expression it ACTS on to a constant. It can convert data that is not of const type to const type, or remove the const attribute.
class A { ... };
void f()
{
const A *pa = new A;//Const object
A *pb;// non Const object
//pb = pa; // There is going to be an error here Const object A pointer is assigned to a non Const object
pb = const_cast<A*>(pa); //OK now
...
}
const char* p = "123";
char* c = const_cast<char*>(p);
c[0] = 1; //The const property is ostensibly compiled away, but the system still does not allow it to be manipulated for its address.
4) reinterpret_cast < T * > (a) the compiler processes at compile time
Any pointer can be converted to any other type of pointer, and 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 such conversions as char* to int*, or One_class* to Unrelated_class*, and so may not be secure.
class A { ... };
class B { ... };
void f()
{
A* pa = new A;
void* pv = reinterpret_cast<A*>(pa);
//Pv now points to an object of type B, which may not be safe
...
}