色www,五月婷婷深爱五月,午夜国产一级片,色噜噜综合,国产大胸无码视频,清纯美女被操黄网站在线观看,波多野结衣av高清一区二区三区

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

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

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

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

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

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

  TYPE b = (TYPE)a

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

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

  reinterpreter_cast 重新解釋類型

  dynamic_cast       命 名上理解是動(dòng)態(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)換,編譯的時(shí)c++編譯器會(huì)做類型檢查;

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

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

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

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

  一般性結(jié)論

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

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

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

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

  3、典型案例

  代碼中包含了4中類型轉(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中類型轉(zhuǎn)化:"<<i1<<endl;

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

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

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

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

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

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

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

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

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

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

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

  • 相關(guān)推薦