<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>

            C++的類型轉換介紹

            時間:2025-08-30 06:42:08 C語言

            C++的類型轉換介紹

              C語言一共只有32個關鍵字,9種控制語句,程序書寫形式自由,區分大小寫。下面是小編分享的C++的類型轉換介紹,一起來看一下吧。

              1、類型轉換名稱和語法

              C風格的強制類型轉換(Type Cast)很簡單,不管什么類型的轉換統統是:

              TYPE b = (TYPE)a

              C++風格的類型轉換提供了4種類型轉換操作符來應對不同場合的應用。

              static_cast             靜態類型轉換。如int轉換成char

              reinterpreter_cast 重新解釋類型

              dynamic_cast       命 名上理解是動態類型轉換。如子類和父類之間的多態類型轉換。

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

              4種類型轉換的格式:

              TYPE B = static_cast<TYPE> (a)

              2、類型轉換一般性介紹

              4中類型轉化介紹

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

              基本類型能轉換 但是不能轉換指針類型

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

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

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

              一般性結論

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

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

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

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

              3、典型案例

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

              #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) /pic/p>

              {

              dog->action();

              dog->doSwim();

              }

              Cat *cat = dynamic_cast<Cat *>(animl);

              if(cat!=NULL) /pic/p>

              {

              cat->action();

              cat->doTree();

              }

              cout<<"func ObjPlay is exit!!! "<<endl;

              }

              /pic/p>

              void Opbuf(const char *p)

              {

              cout << p << endl;

              /pic/p>

              /pic/p>

              char *p2 = const_cast<char*>(p); /pic/p>

              p2[0] = 'b';

              cout << p << endl;

              }

              int main()

              {

              /pic/p>

              double d = 3.14159;

              int i1,i2;

              i1 = d; /pic/p>

              i2 = static_cast<int>(d); /pic/p>

              cout<<"C中類型轉化:"<<i1<<endl;

              cout<<"C++中類型轉化:"<<i2<<endl;

              /pic/p>

              char *p = "abcd";

              int *p1 = NULL;

              int *p2 = NULL;

              p1 = (int *)p; /pic/p>

              /pic/p>

              p2 = reinterpret_cast<int *>(p); /pic/p>

              cout<<"C中類型轉化"<<hex<<*p1<<endl;

              cout<<"C++中類型轉化:"<<hex<<*p2<<endl;

              /pic/p>

              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;

              /pic/p>

              /pic/p>

              char buf[100] = "aaaaaaaaaaaa";

              /pic/p>

              /pic/p>

              /pic/p>

              system("pause");

              return 0;

              }


            【C++的類型轉換介紹】相關文章:

            C++類的轉換02-11

            C語言類型轉換的方法02-21

            java類型的字符轉換的方法02-26

            Java數據類型轉換03-16

            C++中時間與時間戳的轉換11-05

            php數據類型轉換詳解03-17

            C語言數據類型轉換02-28

            C++ 中const和復合類型12-21

            Java中對象類型如何進行轉換11-23

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看