日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

詳解C++中常用的四種類型轉(zhuǎn)換方式_C 語言

作者:lhb2998658795 ? 更新時間: 2022-10-15 編程語言

1.靜態(tài)類型轉(zhuǎn)換:static_cast(exp)

1.1靜態(tài)類型轉(zhuǎn)換主要用于兩種轉(zhuǎn)換環(huán)境

1.1.1 C++內(nèi)置類型的轉(zhuǎn)換:與C風格強轉(zhuǎn)類似。

與c相同的地方:

#include <iostream>
 
using namespace std;
 
int main()
{
    double a=3.14;
 
    cout << static_cast<int>(a) << endl;
 
    return 0;
}

不同的地方就是使用static_cast不能強轉(zhuǎn)內(nèi)置類型指針的,這點可以避免C風格中的越界問題。

如圖所示:

1.1.2當有繼承關系存在時的強轉(zhuǎn):

如果使用static_cast 由子類向父類轉(zhuǎn)型,向上轉(zhuǎn)型,天然安全安全。(應為子類的空間肯定比父類的空間大,子類是在繼承父類的空間上面開辟),代碼如下:

#include <iostream>
 
using namespace std;
 
class A
{
public:
    int a=100;
 
    void Ashow_info()
    {
        cout<<this->a<<endl;
    }
};
 
class B:public A
{
public:
    int a=200;
    int b=300;
    int c=400;
    void Bshow_info()
    {
        cout<<this->a<<this->b<<this->c<<endl;
    }
};
 
 
int main()
{
   B* a=new B;
   static_cast<A*>(a)->Ashow_info();
 
    return 0;
}

結果圖:

我們可以通過子類安全的訪問到父類中的a值。

如果使用static_cast 由父類向子類轉(zhuǎn)型,向下轉(zhuǎn)型,是不安全。

那么何時不安全?何時安全?

不安全的情況介紹:

#include <iostream>
 
using namespace std;
 
class A
{
public:
    int a=100;
 
    void Ashow_info()
    {
        cout<<this->a<<endl;
    }
};
 
class B:public A
{
public:
    int a=200;
    int b=300;
    int c=400;
    void Bshow_info()
    {
        cout<<this->a<<this->b<<this->c<<endl;
    }
};
 
 
int main()
{
   A* a=new A;
   static_cast<B*>(a)->Bshow_info();
 
    return 0;
}

結果圖:

如圖所示結果中并沒有出現(xiàn)本應該打出的200,300,400,這就是不知道子類空間是否被開辟而向下訪問造成的結果。

安全的情況:

#include <iostream>
 
using namespace std;
 
class A
{
public:
    int a=100;
 
    void Ashow_info()
    {
        cout<<this->a<<endl;
    }
};
 
class B:public A
{
public:
    int a=200;
    int b=300;
    int c=400;
    void Bshow_info()
    {
        cout<<this->a<<this->b<<this->c<<endl;
    }
};
 
 
int main()
{
   A* a=new B;
   static_cast<B*>(a)->Bshow_info();
 
    return 0;
}

結果圖:

如圖所示,此時我們可以打出200,300,400,等數(shù)值,說明當我們知道子類空間被開辟時候,就可以安全的向下訪問。

2.動態(tài)類型轉(zhuǎn)換:dynamic_cast(exp)

2.1概念

動態(tài)類型轉(zhuǎn)換是依賴于虛函數(shù)的與繼承關系,沒有虛函數(shù),就無法使用動態(tài)類型轉(zhuǎn)換。dynamic_cast是一個安全類型轉(zhuǎn)換,因為他是依賴于函數(shù)實現(xiàn)動態(tài)轉(zhuǎn)型。因為虛表中的第一個Slot位置保存了類型運行識別信息。

注意使用的條件為:1)要有繼承關系 2)要有虛函數(shù)。

這個虛表的結構:

2.2代碼舉例說明

#include <iostream>
 
using namespace std;
 
class A
{
public:
 
 
    virtual void show_info()
    {
        cout<<"我是父親"<<endl;
    }
};
 
class B:public A
{
public:
 
    void show_info()
    {
        cout<<"我是兒子"<<endl;
    }
};
 
 
int main()
{
   A* a=new B;
   dynamic_cast<B*>(a)->show_info();
 
    return 0;
}

結果圖:

3.常類型轉(zhuǎn)換:const_case(exp)

就是用來修改const修飾的常引用和常指針的轉(zhuǎn)換方式

3.1代碼說明

#include <iostream>
 
using namespace std;
 
int main()
{
    const int& a=100;
 
    const_cast<int&>(a)=200;
 
    cout<<a<<endl;
 
 
    return 0;
}

結果圖:

由圖可知我們修改了常引用的數(shù)值。

4. 解釋類型轉(zhuǎn)換: reinterpret_cast(exp)

4.1概念

這要類型轉(zhuǎn)換方式,是可以慶用于任何類型,他的底層的實現(xiàn)就是對底層二進制數(shù)據(jù)的一個拷貝。所以也是一個不安全的強轉(zhuǎn)。

4.2由于這個一般都不用,從我們最有可能的會用到的情況下抽出來一種,代碼如下:

當我們想把一個數(shù)的地址,用10進制的表達出來的時候,如下,光一個int 是裝不下地址的十進制,所以系統(tǒng)就會給我們報錯。

?這個時候reinterpert_cast就起到了作用,我們可以把他轉(zhuǎn)為long long類型,如下:

#include <iostream>
 
using namespace std;
 
int main()
{
  int a=10;
 
  int *p=&a;
 
 
  cout<<reinterpret_cast<long long>(p)<<endl;
    return 0;
}

結果圖:

原文鏈接:https://blog.csdn.net/a2998658795/article/details/126041434

欄目分類
最近更新