C++中的強制類型轉換:static_cast,reinterpret_cast,dynamic_cast,const_cast

C++中的強制類型轉換


原文轉自:https://blog.csdn.net/qq_29996285/article/details/86508684

關於本文主題還有下面這篇博客可以參考:https://blog.csdn.net/komtao520/article/details/79025562

tip——string轉char*的庫函數方法:

string.c_str();

 

類型轉換

 

靜態轉換(static_cast)

 

使用方式:

static_cast<目標類型>(原始對象)

用法:

  1. 用於父類和子類之間指針或引用的轉換。
  2. 用於基本數據類型之間的轉換,如把int轉換成char,把char轉換成int。這種轉換的安全性也要開發人員來保證。
  3. 沒有父子關係的自定義類型不可以轉換

補充:

  • 進行行轉換(把派生類的指針或引用轉換成基類表示)是安全的;
  • 進行下行轉換(把基類指針或引用轉換成派生類表示)時,由於沒有動態類型檢查,所以是不安全的。

 

基本數據類型轉換的例子:

  1. void test01(){
  2. char a = 'a';
  3. double d = static_cast<double>(a);
  4. cout << "d = " << d <<endl;
  5. }

 

繼承關係指針互相轉換

  1. class Base{};
  2. class Child :public Base{};
  3. class Other{};
  4. void test02(){
  5. Base * base = NULL;
  6. Child * child = NULL;
  7. //把base轉爲 Child*類型 向下 不安全
  8. Child * child2 = static_cast<Child*>(base);
  9. //把child 轉爲 Base* 向上 安全
  10. Base * base2 = static_cast<Base*>(child);
  11. //轉other類型 轉換無效
  12. //Other * other = static_cast<Other*>(base);
  13. }

 

繼承關係引用相互轉換

  1. class Animal{};
  2. class Dog : public Animal{};
  3. class Other{};
  4. void test03(){
  5. Animal ani_ref;
  6. Dog dog_ref;
  7. //繼承關係指針轉換
  8. Animal& animal01 = ani_ref;
  9. Dog& dog01 = dog_ref;
  10. //子類指針轉成父類指針,安全
  11. Animal& animal02 = static_cast<Animal&>(dog01);
  12. //父類指針轉成子類指針,不安全
  13. Dog& dog02 = static_cast<Dog&>(animal01);
  14. }

 

 

 

動態轉換(dynamic_cast)——十分嚴格

 

使用方式:

dynamic_cast<目標類型>(原始對象)

用法:

  1. 基礎類型不可以轉換
  2. dynamic_cast主要用於類層次間上行轉換和下行轉換;
  3. 在類層次間進行上行轉換時,dynamic_cast和static_cast的效果是一樣的;
  4. 在進行下行轉換時,dynamic_cast具有類型檢查的功能,比static_cast更安全
  5. 發生多態,則允許發生向上轉換和向下轉換。

![在這裏插入圖片描述](https://img-blog.csdnimg.cn/20190116161605972.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5OTk2Mjg1,size_16,color_FFFFFF,t_70)

dynamic_cast 如果發生了多態,那麼可以讓基類轉爲派生類 ,向下轉換

原因:

創建空間大小的時候,是按照子類的大小進行創建的。

使用樣例:

  1. class Animal {
  2. public:
  3. virtual void ShowName() = 0;
  4. };
  5. class Dog : public Animal{
  6. virtual void ShowName(){
  7. cout << "I am a dog!" << endl;
  8. }
  9. };
  10. class Other {
  11. public:
  12. void PrintSomething(){
  13. cout << "我是其他類!" << endl;
  14. }
  15. };
  16. //普通類型轉換
  17. void test01(){
  18. //不支持基礎數據類型
  19. int a = 10;
  20. //double a = dynamic_cast<double>(a);
  21. }
  22. //繼承關係指針
  23. void test02(){
  24. Animal* animal01 = NULL;
  25. Dog* dog01 = new Dog;
  26. //子類指針轉換成父類指針 可以
  27. Animal* animal02 = dynamic_cast<Animal*>(dog01);
  28. animal02->ShowName();
  29. //父類指針轉換成子類指針 不可以
  30. //Dog* dog02 = dynamic_cast<Dog*>(animal01);
  31. }
  32. //繼承關係引用
  33. void test03(){
  34. Dog dog_ref;
  35. Dog& dog01 = dog_ref;
  36. //子類引用轉換成父類引用 可以
  37. Animal& animal02 = dynamic_cast<Animal&>(dog01);
  38. animal02.ShowName();
  39. }
  40. //無繼承關係指針轉換
  41. void test04(){
  42. Animal* animal01 = NULL;
  43. Other* other = NULL;
  44. //不可以
  45. //Animal* animal02 = dynamic_cast<Animal*>(other);
  46. }

 

 

 

 

常量轉換(const_cast)

 

該運算符用來修改類型的const屬性

 

使用方式:

  1. 加上const: const int * newP2  =  const_cast<const int *>(p2);
  2. 去掉const:            int * newp    =  const_cast<int *>(p);

用法:

  1. 常量引用被轉換成非常量引用,並且仍然指向原來的對象;
  2. 常量指針被轉化成非常量指針,並且仍然指向原來的對象;
  3. 不能直接非指針非引用的變量使用const_cast操作符去直接移除它的const.
  1. //常量指針轉換成非常量指針
  2. void test01(){
  3. const int* p = NULL;
  4. int* np = const_cast<int*>(p);
  5. int* pp = NULL;
  6. const int* npp = const_cast<const int*>(pp);
  7. const int a = 10; //不能對非指針或非引用進行轉換
  8. //int b = const_cast<int>(a); }
  9. //常量引用轉換成非常量引用
  10. void test02(){
  11. int num = 10;
  12. int & refNum = num;
  13. const int& refNum2 = const_cast<const int&>(refNum);
  14. }

 

 

 

重新解釋轉換(reinterpret_cast)——最不安全,不推薦

 

主要用於將一種數據類型從一種類型轉換爲另一種類型。它可以將一個指針轉換成一個整數,也可以將一個整數轉換成一個指針.

  1. void test06(){
  2. int a = 10;
  3. int * p = reinterpret_cast<int *>(a);
  4. Base * base = NULL;
  5. Other * other = reinterpret_cast<Other*>(base);
  6. //最不安全 ,不推薦
  7. }

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章