夜间福利网站,免费动漫av,一级做a爰片久久毛片免费陪,夜夜骑首页,黄色毛片视频,插插插操操操,综合av色

C++的類(lèi)型轉(zhuǎn)換介紹

時(shí)間:2025-08-30 06:42:08 C語(yǔ)言

C++的類(lèi)型轉(zhuǎn)換介紹

  C語(yǔ)言一共只有32個(gè)關(guān)鍵字,9種控制語(yǔ)句,程序書(shū)寫(xiě)形式自由,區(qū)分大小寫(xiě)。下面是小編分享的C++的類(lèi)型轉(zhuǎn)換介紹,一起來(lái)看一下吧。

  1、類(lèi)型轉(zhuǎn)換名稱(chēng)和語(yǔ)法

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

  TYPE b = (TYPE)a

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

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

  reinterpreter_cast 重新解釋類(lèi)型

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

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

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

  TYPE B = static_cast<TYPE> (a)

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

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

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

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

  2)若不同類(lèi)型之間,進(jìn)行強(qiáng)制類(lèi)型轉(zhuǎn)換,用reinterpret_cast<>() 進(jìn)行重新解釋

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

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

  一般性結(jié)論

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

  C語(yǔ)言中不能隱式類(lèi)型轉(zhuǎn)換的,在c++中可以用 reinterpret_cast<>() 進(jìn)行強(qiáng)行類(lèi)型 解釋。

  static_cast<>()和reinterpret_cast<>() 基本上把C語(yǔ)言中的 強(qiáng)制類(lèi)型轉(zhuǎn)換給覆蓋

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

  3、典型案例

  代碼中包含了4中類(lèi)型轉(zhuǎn)化的實(shí)例,以及注意點(diǎ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) /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中類(lèi)型轉(zhuǎn)化:"<<i1<<endl;

  cout<<"C++中類(lèi)型轉(zhuǎn)化:"<<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中類(lèi)型轉(zhuǎn)化"<<hex<<*p1<<endl;

  cout<<"C++中類(lèi)型轉(zhuǎn)化:"<<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++的類(lèi)型轉(zhuǎn)換介紹】相關(guān)文章:

C++類(lèi)的轉(zhuǎn)換02-11

C語(yǔ)言類(lèi)型轉(zhuǎn)換的方法02-21

java類(lèi)型的字符轉(zhuǎn)換的方法02-26

Java數(shù)據(jù)類(lèi)型轉(zhuǎn)換03-16

C++中時(shí)間與時(shí)間戳的轉(zhuǎn)換11-05

php數(shù)據(jù)類(lèi)型轉(zhuǎn)換詳解03-17

C語(yǔ)言數(shù)據(jù)類(lèi)型轉(zhuǎn)換02-28

C++ 中const和復(fù)合類(lèi)型12-21

Java中對(duì)象類(lèi)型如何進(jìn)行轉(zhuǎn)換11-23