關於本文主題還有下面這篇博客可以參考:https://blog.csdn.net/komtao520/article/details/79025562
tip——string轉char*的庫函數方法:
string.c_str();
類型轉換
靜態轉換(static_cast)
使用方式:
static_cast<目標類型>(原始對象)
用法:
- 用於父類和子類之間指針或引用的轉換。
- 用於基本數據類型之間的轉換,如把int轉換成char,把char轉換成int。這種轉換的安全性也要開發人員來保證。
- 沒有父子關係的自定義類型不可以轉換
補充:
- 進行上行轉換(把派生類的指針或引用轉換成基類表示)是安全的;
- 進行下行轉換(把基類指針或引用轉換成派生類表示)時,由於沒有動態類型檢查,所以是不安全的。
基本數據類型轉換的例子:
- void test01(){
- char a = 'a';
- double d = static_cast<double>(a);
- cout << "d = " << d <<endl;
- }
繼承關係指針互相轉換
- class Base{};
- class Child :public Base{};
- class Other{};
- void test02(){
- Base * base = NULL;
- Child * child = NULL;
-
- //把base轉爲 Child*類型 向下 不安全
- Child * child2 = static_cast<Child*>(base);
-
- //把child 轉爲 Base* 向上 安全
- Base * base2 = static_cast<Base*>(child);
-
- //轉other類型 轉換無效
- //Other * other = static_cast<Other*>(base);
- }
繼承關係引用相互轉換
- class Animal{};
- class Dog : public Animal{};
- class Other{};
-
- void test03(){
-
- Animal ani_ref;
- Dog dog_ref;
- //繼承關係指針轉換
- Animal& animal01 = ani_ref;
- Dog& dog01 = dog_ref;
- //子類指針轉成父類指針,安全
- Animal& animal02 = static_cast<Animal&>(dog01);
- //父類指針轉成子類指針,不安全
- Dog& dog02 = static_cast<Dog&>(animal01);
- }
動態轉換(dynamic_cast)——十分嚴格
使用方式:
dynamic_cast<目標類型>(原始對象)
用法:
- 基礎類型不可以轉換
- dynamic_cast主要用於類層次間的上行轉換和下行轉換;
- 在類層次間進行上行轉換時,dynamic_cast和static_cast的效果是一樣的;
- 在進行下行轉換時,dynamic_cast具有類型檢查的功能,比static_cast更安全;
- 發生多態,則允許發生向上轉換和向下轉換。
![在這裏插入圖片描述](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 如果發生了多態,那麼可以讓基類轉爲派生類 ,向下轉換
原因:
創建空間大小的時候,是按照子類的大小進行創建的。
使用樣例:
- class Animal {
- public:
- virtual void ShowName() = 0;
- };
- class Dog : public Animal{
- virtual void ShowName(){
- cout << "I am a dog!" << endl;
- }
- };
- class Other {
- public:
- void PrintSomething(){
- cout << "我是其他類!" << endl;
- }
- };
-
- //普通類型轉換
- void test01(){
-
- //不支持基礎數據類型
- int a = 10;
- //double a = dynamic_cast<double>(a);
- }
-
- //繼承關係指針
- void test02(){
-
- Animal* animal01 = NULL;
- Dog* dog01 = new Dog;
-
- //子類指針轉換成父類指針 可以
- Animal* animal02 = dynamic_cast<Animal*>(dog01);
- animal02->ShowName();
- //父類指針轉換成子類指針 不可以
- //Dog* dog02 = dynamic_cast<Dog*>(animal01);
- }
-
- //繼承關係引用
- void test03(){
-
- Dog dog_ref;
- Dog& dog01 = dog_ref;
-
- //子類引用轉換成父類引用 可以
- Animal& animal02 = dynamic_cast<Animal&>(dog01);
- animal02.ShowName();
- }
-
- //無繼承關係指針轉換
- void test04(){
-
- Animal* animal01 = NULL;
- Other* other = NULL;
-
- //不可以
- //Animal* animal02 = dynamic_cast<Animal*>(other);
- }
常量轉換(const_cast)
該運算符用來修改類型的const屬性。
使用方式:
- 加上const: const int * newP2 = const_cast<const int *>(p2);
- 去掉const: int * newp = const_cast<int *>(p);
用法:
- 常量引用被轉換成非常量引用,並且仍然指向原來的對象;
- 常量指針被轉化成非常量指針,並且仍然指向原來的對象;
- 不能直接對非指針和非引用的變量使用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)——最不安全,不推薦
主要用於將一種數據類型從一種類型轉換爲另一種類型。它可以將一個指針轉換成一個整數,也可以將一個整數轉換成一個指針.
- void test06(){
- int a = 10;
- int * p = reinterpret_cast<int *>(a);
-
- Base * base = NULL;
- Other * other = reinterpret_cast<Other*>(base);
-
- //最不安全 ,不推薦
- }