Four types of coercion for C + + are converted to: static_cast, Const_cast, reinterpret_cast, and dynamic_cast
General form of type conversion: Cast-name
Static_cast
Any explicitly defined type conversions can use static_cast as long as they do not contain the underlying const;
Double slope = static_cast
Note:
Top-Level const: Indicates that the pointer itself is a constant. such as: int *const p;
Underlying const: Indicates that the object that the pointer refers to is a constant. such as: int const *p;
Const_cast
This operator can only change the underlying const of an operand.
#include<iostream> using namespace std;int main() { const char *pc=" HDU"; char *p=const_cast<char *>(pc); //正确, cout<<"hello"<<p<<endl; return 0; }
Note: Here can only use const_cast, but not with static_cast;
Reinterpret_cast
Typically, the bit patterns of the operands provide a lower level of re-interpretation.
Note:
1. Convert between pointers, convert one type of pointer to another type of pointer, irrelevant type;
2. Converts the pointer value to an integer number, but not to a non-pointer-type conversion.
dynamic_cast
Pointers and references for objects only, cannot be used for casting of built-in base data types.
If you use dynamic_cast to convert, you must have a virtual function in the base class, or the compilation will not pass.
Run-time type recognition, a pointer or reference that is used to safely convert a pointer or reference to a base class to a derived class.
dynamic_cast the pointer, fails to return NULL, successfully returns the normal cast of the object pointer;
Dynamic_cast the reference, the failure throws an exception bad_cast, which successfully returns the normal cast object reference.
For an "up conversion" (that is, a derived class pointer or reference type is converted to its base class type), either the pointer or the reference upward conversion is safe.
There are two scenarios for "down transition":
1, the base class pointer refers to the object is a derived class type, the conversion is safe;
2, the base class pointer refers to the object is the base class type, in this case dynamic_cast at run time to do the check, the conversion fails, the return result is 0;
On the reference, dynamic_cast is still often used for "safe downward transformation". As with pointers, the downward transformation of a reference can also be divided into two cases, unlike pointers, where there is no null reference, so a Bad_cast exception is thrown when the referenced dynamic_cast detection fails.
#include <iostream>#include <cstring>using namespace std;class A{public: virtual void f() { cout << "hello" << endl; }}; class B: public A{public: void f() { cout << "hello2" << endl; } };int main(){ A* a1=new B;//a1是A类型的指针指向一个B类型的对象 A* a2=new A;//a2是A类型的指针指向一个A类型的对象 B* b; b=dynamic_cast<B*>(a1); //结果为not null,向下转换成功,a1之前指向的就是B类型的对象,所以可以转换成B类型的指针。 if(b==NULL) cout<<"null"<<endl; else cout<<"not null"<<endl; b=dynamic_cast<B*>(a2);//结果为null,向下转换失败 if(b==NULL) cout<<"null"<<endl; else cout<<"not null"<<endl; return 0;}
Summarize
Basic type conversion with static_cast.
Go to the const attribute with const_cast.
Different types of pointer-type conversions with Reinterpreter_cast.
A type conversion between polymorphic classes is used with Daynamic_cast.
4 types of coercion type conversions in C + +
Four types of coercion for C + + are converted to: static_cast, Const_cast, reinterpret_cast, and dynamic_cast
General form of type conversion: Cast-name
Static_cast
Any explicitly defined type conversions can use static_cast as long as they do not contain the underlying const;
Double slope = static_cast
Note:
Top-Level const: Indicates that the pointer itself is a constant. such as: int *const p;
Underlying const: Indicates that the object that the pointer refers to is a constant. such as: int const *p;
Const_cast
This operator can only change the underlying const of an operand.
#include<iostream> using namespace std;int main() { const char *pc=" HDU"; char *p=const_cast<char *>(pc); //正确, cout<<"hello"<<p<<endl; return 0; }
Note: Here can only use const_cast, but not with static_cast;
Reinterpret_cast
Typically, the bit patterns of the operands provide a lower level of re-interpretation.
Note:
1. Convert between pointers, convert one type of pointer to another type of pointer, irrelevant type;
2. Converts the pointer value to an integer number, but not to a non-pointer-type conversion.
dynamic_cast
Pointers and references for objects only, cannot be used for casting of built-in base data types.
If you use dynamic_cast to convert, you must have a virtual function in the base class, or the compilation will not pass.
Run-time type recognition, a pointer or reference that is used to safely convert a pointer or reference to a base class to a derived class.
dynamic_cast the pointer, fails to return NULL, successfully returns the normal cast of the object pointer;
Dynamic_cast the reference, the failure throws an exception bad_cast, which successfully returns the normal cast object reference.
For an "up conversion" (that is, a derived class pointer or reference type is converted to its base class type), either the pointer or the reference upward conversion is safe.
There are two scenarios for "down transition":
1, the base class pointer refers to the object is a derived class type, the conversion is safe;
2, the base class pointer refers to the object is the base class type, in this case dynamic_cast at run time to do the check, the conversion fails, the return result is 0;
On the reference, dynamic_cast is still often used for "safe downward transformation". As with pointers, the downward transformation of a reference can also be divided into two cases, unlike pointers, where there is no null reference, so a Bad_cast exception is thrown when the referenced dynamic_cast detection fails.
#include <iostream>#include <cstring>using namespace std;class A{public: virtual void f() { cout << "hello" << endl; }}; class B: public A{public: void f() { cout << "hello2" << endl; } };int main(){ A* a1=new B;//a1是A类型的指针指向一个B类型的对象 A* a2=new A;//a2是A类型的指针指向一个A类型的对象 B* b; b=dynamic_cast<B*>(a1); //结果为not null,向下转换成功,a1之前指向的就是B类型的对象,所以可以转换成B类型的指针。 if(b==NULL) cout<<"null"<<endl; else cout<<"not null"<<endl; b=dynamic_cast<B*>(a2);//结果为null,向下转换失败 if(b==NULL) cout<<"null"<<endl; else cout<<"not null"<<endl; return 0;}
Summarize
Basic type conversion with static_cast.
Go to the const attribute with const_cast.
Different types of pointer-type conversions with Reinterpreter_cast.
A type conversion between polymorphic classes is used with Daynamic_cast.
http://www.frankyang.cn/2017/05/10/c-4%E7%A7%8D%E5%BC%BA%E5%88%B6%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/
4 types of coercion type conversions in C + +