concept
The meaning of type conversion is to change the representation of a variable by changing its type to another type. In order to type one simple object into another you will use the type conversion operator.
C-style cast is very simple, no matter what type of cast is: TYPE b = (TYPE)a
C + + style type conversion provides four types of conversion operators to cope with different occasions.
type | usage |
---|---|
static_cast | General conversion |
dynami_cast | Commonly used when converting between base and derived classes |
const_cast | Mainly for the conversion of const |
reinterpert_cast | Used for conversion without any association, such as conversion of a character pointer to an integer |
Code:
#include<iostream>
using namespace std;
class Building{};
class Animal{};
class Cat :public Animal{};
//static_cast
void test01(){
int a = 97;
char c = static_cast<char>(a);
cout << "c = " << c << endl;//Output ASCII value a
//Pointer to the underlying data type
int *p = NULL;
//char* sp = static_cast<char*>(p);//error
//Object pointer
Building* building = NULL;
//Animal* ani = static_cast<Animal*>(building);//error
//Convert object pointer with inheritance relationship
//Parent class pointer rotor class pointer
Animal* ani = NULL;
Cat* cat = static_cast<Cat*>(ani);//ok
//Child pointer converted to parent pointer
Cat* soncat = NULL;
Animal* anifather = static_cast<Animal*>(soncat);//ok
Animal aniobj;
Animal& aniref = aniobj;
Cat& cat2 = static_cast<Cat&>(aniref);//ok
Cat catobj;
Cat& catref = catobj;
Animal& anifather2 = static_cast<Animal&>(catref);//ok
/*static_cast For built-in data types,
There are also pointers or references with inheritance relationships*/
}
//A pointer or reference with an inheritance relationship in a dynamic cast transformation will be checked for object type before the transformation
void test02()
{
//Basic data type cannot be converted
int a = 10;;
//char c = dynamic_cast<char>(a);//error
//Pointer to non inheritance relationship
Animal* ani = NULL;
//Building* building = dynamic_cast<Building*>(ani);//error
//With inheritance relationship pointer
//Animal* ani = NULL;
//Cat* cat = dynamic_cast<Cat*>(ani);//error
/*The reason lies in the type security check done by dynamic cast
The sub class pointer can be converted to the parent class pointer (from large to small), which is type safe, because the sub class contains the elements of the parent class, the pointer scope is large, and the pointer will not cross the boundary
The parent pointer is converted to the child pointer (from small to small). The type is not safe, and the pointer will cross the boundary*/
Cat* cat = NULL;
Animal* ani2 = dynamic_cast<Animal*>(cat);
/*Conclusion:
dynamic_cast Only pointers or references with inheritance relationships can be converted, and
Can only convert from subtype to base type*/
}
//Const? Cast pointer reference or object pointer
void test03()
{
//1 basic data type
int a = 10;
const int& b = a;
//b=10;//error cannot be changed
int& c = const_cast<int&>(b);//const property cancel
c = 20;
cout << "a:" << a << endl;
cout << "b:" << b << endl;
cout << "c:" << c << endl;
/*Output:
a:20
b:20
c:20
*/
//Pointer
const int* p = NULL;
int* p2 = const_cast<int*>(p);//OK
int* p3 = NULL;
const int* p4 = const_cast<const int*>(p3);//Add const attribute
/*Conclusion:
Add or remove const attribute of variable*/
}
//Reinterpret \ cast castcasts irrelevant pointer types, including function pointers
typedef void(*FUNC1)(int, int);
typedef int(*FUNC2)(int, char*);
void test04()
{
//1. All irrelevant pointer types can be converted
Building* building = NULL;
Animal* ani = reinterpret_cast<Animal*>(building);
//2. Function pointer conversion
FUNC1 func1;
FUNC2 func2 = reinterpret_cast<FUNC2>(func1);
}
/*summary
Conclusion 1: the programmer must be clear about the variables to be transformed, the types before and after transformation, and
What is the result of the conversion
Conclusion 2: generally, type conversion is not recommended to avoid type conversion*/
int main()
{
cout<<"hello world"<<endl;
test01();
test03();
return 0;
}
summary
Conclusion 1: the programmer must be clear about the variables to be transformed, the types before and after transformation, and What is the result of the conversion; Conclusion 2: generally, type conversion is not recommended to avoid type conversion;