We generally use type conversion in C as follows:
int a=1;
double b=(double)a;
或者double b=double(a);
The class mechanism is added to C++, and a class can also be regarded as a type, so these types can also be converted to each other. Of course, in order to be compatible with C, C++ also retains the above-mentioned type conversion method in C. In addition, Four type conversion mechanisms are also introduced in C++: static_cast, dynamic_cast, const_cast, reinterpret_cast
1、static_cast
static_cast supports all compiler-approved implicit type conversions
<1> Conversion of basic data types
int a = 6;
doubleb = static_cast<int>(a);
double d = 3.14159265;
inti = static_cast<int>(d);
<2> Convert the derived class pointer/reference to the base class pointer/reference
class base{ ….}
class derived : public base{….}
base *b;
derived *d = new derived();
b = static_cast<base *>(d);
<3> Convert the base class pointer/reference to the derived class pointer/reference (Note, no type safety check is performed here, the difference from dynamic_cast)
class Base {};
class Derived : public Base {};
Base *a = new Base;
Derived *b = static_cast<Derived *>(a);
2、dynamic_cast
dynamic_cast is only used to perform conversions between derived and base classes
<1> Convert the derived class to the base class
class base{ ….}
class derived : public base{….}
base *b;
derived *d = new derived();
b = dynamic_cast<base *>(d);
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'
Derived d1 = dynamic_cast<Derived &*>(b1);// succeeds
Derived d2 = dynamic_cast<Derived &*>(b2);// fails: exception thrown
Among them, pay attention to the line Derived * d2 = dynamic_cast <Derived *>(b2); Compare this line with Derived *b = static_cast <Derived *>(a); in static_cast , you can find that dynamic_cast performs type conversion Security check<1> The other three military compilations are completed, dynamic_cast is processed at runtime, and runtime type checking is required at runtime
比如:Derived* d2 = dynamic_cast<Derived *>(b2);// fails: returns 'NULL'
<2> There must be a virtual function in the base class, because the type information of the runtime type check is in the virtual function table, and there is a virtual function table only if there is a virtual function.
<3> Up and down transformation of classes can be achieved, provided that public or protected inheritance must be used
3、const_cast
You can only remove or add const attributes to pointers or references of the same type
Cannot use const_cast between variable types
Cannot be used for conversion between different types, only const attributes of the same type can be changed
eg1. const int c=0;
const int *a=&c;
int * b= const_cast < int *>(a); ////Remove constness in pointers
eg2. int s=0;
int *pi = &s;
*pi=2;
const int * pii = const_cast < int *>(pi); //Add pointer constness;
*pii=3;//error
cout<<*pii<<endl;
4、interpret_cast<1> Can realize conversion between any two types
<2> You can convert a pointer of a type to a pointer of other types, or convert the pointer type to an integer, and vice versa
<3> It is just a binary copy of a pointer to another pointer, and the content pointed to between types does not do any type checking and conversion
class A {};
class B {};
A * a = new A;
B * b = reinterpret_cast<B *>(a);