类型转换(cast)是将一种数据类型转换成另一种数据类型。例如,如果将一个整型值赋给一个浮点类型的变量,编译器会暗地里将其转换成浮点类型。
转换是非常有用的,但是它也会带来一些问题,比如在转换指针时,我们很可能将其转换成一个比它更大的类型,但这可能会破坏其他的数据。
应该小心类型转换,因为转换也就相当于对编译器说:忘记类型检查,把它看做其他的类型。
一般情况下,尽量少的去使用类型转换,除非用来解决非常特殊的问题。

无论什么原因,任何一个程序如果使用很多类型转换都值得怀疑

标准c++提供了一个显示的转换的语法,来替代旧的C风格的类型转换。
使用C风格的强制转换可以把想要的任何东西转换成我们需要的类型。那为什么还需要一个新的C++类型的强制转换呢?
新类型的强制转换可以提供更好的控制强制转换过程,允许控制各种不同种类的强制转换。C++风格的强制转换其他的好处是,它们能更清晰的表明它们要干什么。程序员只要扫一眼这样的代码,就能立即知道一个强制转换的目的。

静态转换(static_cast)

  • 用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换
    • 进行上行转换(把派生类的指针或引用转换成基类表示)是安全的
    • 进行下行转换(把基类指针或引用转换成派生类表示)时,由于没有动态类型检查,所以是不安全的
  • 用于基本数据类型之间的转换,如把int转换成char,把char转换成int。这种转换的安全性也要开发人员来保证 ```cpp class Animal{}; class Dog : public Animal{}; class Other{};

//基础数据类型转换 void test01(){ char a = ‘a’; double b = static_cast(a); }

//继承关系指针互相转换 void test02(){ //继承关系指针转换 Animal animal01 = NULL; Dog dog01 = NULL; //子类指针转成父类指针,安全 Animal animal02 = static_cast<Animal>(dog01); //父类指针转成子类指针,不安全 Dog dog02 = static_cast<Dog>(animal01); }

//继承关系引用相互转换 void test03(){

  1. Animal ani_ref;
  2. Dog dog_ref;
  3. //继承关系指针转换
  4. Animal& animal01 = ani_ref;
  5. Dog& dog01 = dog_ref;
  6. //子类指针转成父类指针,安全
  7. Animal& animal02 = static_cast<Animal&>(dog01);
  8. //父类指针转成子类指针,不安全
  9. Dog& dog02 = static_cast<Dog&>(animal01);

} //无继承关系指针转换 void test04(){

  1. Animal* animal01 = NULL;
  2. Other* other01 = NULL;
  3. //转换失败
  4. //Animal* animal02 = static_cast<Animal*>(other01);

}

  1. <a name="D97Ec"></a>
  2. ## 动态转换(dynamic_cast)
  3. - `dynamic_cast`主要用于类层次间的上行转换和下行转换
  4. - 在类层次间进行上行转换时,`dynamic_cast`和`static_cast`的效果是一样的
  5. - 在进行下行转换时,`dynamic_cast`具有类型检查的功能,比`static_cast`更安全
  6. ```cpp
  7. class Animal {
  8. public:
  9. virtual void ShowName() = 0;
  10. };
  11. class Dog : public Animal{
  12. virtual void ShowName(){
  13. cout << "I am a dog!" << endl;
  14. }
  15. };
  16. class Other {
  17. public:
  18. void PrintSomething(){
  19. cout << "我是其他类!" << endl;
  20. }
  21. };
  22. //普通类型转换
  23. void test01(){
  24. //不支持基础数据类型
  25. int a = 10;
  26. //double a = dynamic_cast<double>(a);
  27. }
  28. //继承关系指针
  29. void test02(){
  30. Animal* animal01 = NULL;
  31. Dog* dog01 = new Dog;
  32. //子类指针转换成父类指针 可以
  33. Animal* animal02 = dynamic_cast<Animal*>(dog01);
  34. animal02->ShowName();
  35. //父类指针转换成子类指针 不可以
  36. //Dog* dog02 = dynamic_cast<Dog*>(animal01);
  37. }
  38. //继承关系引用
  39. void test03(){
  40. Dog dog_ref;
  41. Dog& dog01 = dog_ref;
  42. //子类引用转换成父类引用 可以
  43. Animal& animal02 = dynamic_cast<Animal&>(dog01);
  44. animal02.ShowName();
  45. }
  46. //无继承关系指针转换
  47. void test04(){
  48. Animal* animal01 = NULL;
  49. Other* other = NULL;
  50. //不可以
  51. //Animal* animal02 = dynamic_cast<Animal*>(other);
  52. }

常量转换(const_cast)

该运算符用来修改类型的const属性。

  • 常量指针被转化成非常量指针,并且仍然指向原来的对象
  • 常量引用被转换成非常量引用,并且仍然指向原来的对象

    注意:不能直接对非指针和非引用的变量使用const_cast操作符去直接移除它的const

//常量指针转换成非常量指针
void test01(){

    const int* p = NULL;
    int* np = const_cast<int*>(p);

    int* pp = NULL;
    const int* npp = const_cast<const int*>(pp);

    const int a = 10;  //不能对非指针或非引用进行转换
    //int b = const_cast<int>(a); }

//常量引用转换成非常量引用
void test02(){

int num = 10;
    int & refNum = num;

    const int& refNum2 = const_cast<const int&>(refNum);

}

重新解释转换(reinterpret_cast)

这是最不安全的一种转换机制,最有可能出问题。
主要用于将一种数据类型从一种类型转换为另一种类型。它可以将一个指针转换成一个整数,也可以将一个整数转换成一个指针。