欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++的類型轉(zhuǎn)換詳細介紹

 更新時間:2017年06月16日 10:02:34   投稿:lqh  
這篇文章主要介紹了C++的類型轉(zhuǎn)換詳細介紹的相關(guān)資料,需要的朋友可以參考下

C++的類型轉(zhuǎn)換詳細介紹

1、類型轉(zhuǎn)換名稱和語法

    C風(fēng)格的強制類型轉(zhuǎn)換(Type Cast)很簡單,不管什么類型的轉(zhuǎn)換統(tǒng)統(tǒng)是:

    TYPE b = (TYPE)a  

    C++風(fēng)格的類型轉(zhuǎn)換提供了4種類型轉(zhuǎn)換操作符來應(yīng)對不同場合的應(yīng)用。

   static_cast             靜態(tài)類型轉(zhuǎn)換。如int轉(zhuǎn)換成char

           reinterpreter_cast 重新解釋類型

      dynamic_cast       命 名上理解是動態(tài)類型轉(zhuǎn)換。如子類和父類之間的多態(tài)類型轉(zhuǎn)換。

           const_cast           字面上理解就是去const屬性。

    4種類型轉(zhuǎn)換的格式:

    TYPE B = static_cast<TYPE> (a) 

2、類型轉(zhuǎn)換一般性介紹

    4中類型轉(zhuǎn)化介紹 

  1)static_cast<>() 靜態(tài)類型轉(zhuǎn)換,編譯的時c++編譯器會做類型檢查;

        基本類型能轉(zhuǎn)換 但是不能轉(zhuǎn)換指針類型

    2)若不同類型之間,進行強制類型轉(zhuǎn)換,用reinterpret_cast<>() 進行重新解釋

    3)dynamic_cast<>(),動態(tài)類型轉(zhuǎn)換,安全的基類和子類之間轉(zhuǎn)換;運行時類型檢查 (C++特有的)

    4)const_cast<>(),去除變量的只讀屬性(C++特有的),變量的類型必須是指針,指針指向的內(nèi)存空間可被修改

    一般性結(jié)論

    C語言中  能隱式類型轉(zhuǎn)換的,在c++中可用 static_cast<>()進行類型轉(zhuǎn)換。因C++編譯器在編譯檢查一般都能通過;

    C語言中不能隱式類型轉(zhuǎn)換的,在c++中可以用 reinterpret_cast<>() 進行強行類型 解釋。

    static_cast<>()和reinterpret_cast<>() 基本上把C語言中的 強制類型轉(zhuǎn)換給覆蓋

    reinterpret_cast<>()很難保證移植性。

3、典型案例

代碼中包含了4中類型轉(zhuǎn)化的實例,以及注意點。 

#include<iostream>
using namespace std;

class Animal
{
public:
 virtual void action()
 {
 cout<<"the action is animal's "<<endl;
 }
};

class Dog:public Animal
{
public:
 virtual void action()
 {
 cout<<"the action is dog's "<<endl;
 }

 void doSwim()
 {
 cout<<"the dog is swimming..."<<endl;
 }
};

class Cat:public Animal
{
public:
 virtual void action()
 {
 cout<<"the action is cat's "<<endl;
 }

 void doTree()
 {
 cout<<"the cat is claming tree..."<<endl;
 }
};

class Desk
{
public:
 void action()
 {
 cout<<"this is Desk, not belong Animal"<<endl;
 }
};

void ObjPlay(Animal *animl)
{
 animl->action();
 Dog *dog = dynamic_cast<Dog *>(animl);
 if(dog!=NULL) //判斷是不是dog
 {
 dog->action();
 dog->doSwim();
 }

 Cat *cat = dynamic_cast<Cat *>(animl);
 if(cat!=NULL) //判斷是不是cat
 {
 cat->action();
 cat->doTree();
 }
 cout<<"func ObjPlay is exit!!!\n"<<endl;
}

//典型用法 把形參的只讀屬性去掉
void Opbuf(const char *p)
{
 cout << p << endl;
 //char *p2 = p; err:const char *不能初始化為char *
 //p[0] = 'b'; err:必須是可修改的左值
 char *p2 = const_cast<char*>(p); //去除只讀的屬相
 p2[0] = 'b';
 cout << p << endl;
}

int main()
{
 //靜態(tài)類型轉(zhuǎn)化 static_cast<>()
 double d = 3.14159;
 int i1,i2;
 i1 = d; //C中的隱式類型轉(zhuǎn)化
 i2 = static_cast<int>(d); //C++中的靜態(tài)類型轉(zhuǎn)化
 cout<<"C中類型轉(zhuǎn)化:"<<i1<<endl;
 cout<<"C++中類型轉(zhuǎn)化:"<<i2<<endl;


 //重新解釋類型reinterpret_cast<>()
 char *p = "abcd";
 int *p1 = NULL;
 int *p2 = NULL;
 p1 = (int *)p; //C中強制類型轉(zhuǎn)化
 //p2 = static_cast<int *>(p);  編譯報錯,類型轉(zhuǎn)化錯誤,靜態(tài)類型不能轉(zhuǎn)化指針
 p2 = reinterpret_cast<int *>(p); //C++中的重新解釋類型
 cout<<"C中類型轉(zhuǎn)化"<<hex<<*p1<<endl;
 cout<<"C++中類型轉(zhuǎn)化:"<<hex<<*p2<<endl;

 //動態(tài)類型轉(zhuǎn)換 dynamic_cast<>()
 Animal an;
 Animal *pAn = &an;
 ObjPlay(pAn);

 Dog dog;
 Dog *pDog = &dog;
 ObjPlay(pDog);

 Cat cat;
 Cat *pCat = &cat;
 ObjPlay(pCat);

 Desk desk;
 Desk *pDesk = &desk;
 //Animal *pAn = dynamic_cast<Animal*>(pDesk); 不同的基類指針之間不能相互轉(zhuǎn)化,安全

 //去除變量的只讀屬性,const_cast<>(),此類型必須是指針
 char buf[100] = "aaaaaaaaaaaa";
 //Opbuf(buf);
 //要保證指針所執(zhí)行的內(nèi)存空間能修改才行 若不能修改 還是會引起程序異常
 //Opbuf("dddddddddddsssssssssssssss");

 system("pause");
 return 0;
}
 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論