2、https://blog.csdn.net/zhouwei1221q/article/details/44978361
一、主要应用于以下情形:
1、用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换。
进行上行转换(把派生类的指针或引用转换成基类表示)是安全的;
进行下行转换(把基类指针或引用转换成派生类表示)时,由于没有动态类型检查,所以是不安全的。
2、用于基本数据类型之间的转换,如把int转换成char,把int转换成enum。
3、把空指针转换成目标类型的空指针。
4、把任何类型的表达式转换成void类型。
二、转换
1、类转换:
Class Partent{};
Class Child : public Partent {};
Child *a = new Child();
Partent *b = static_cast<Partent *>(a);(上行转换是安全的)
Partent *b = new Partent();
Child *a = static_cast<Child*>(b);(下行转换是不安全的,不能使用)
2、基本类型的转换
int a = 10;
float b = static_cast<float>(a);
3、void *类型的转换
int a = 10;
void *p = &a;
int *i = static_cast<int *>(p);