一级日韩免费大片,亚洲一区二区三区高清,性欧美乱妇高清come,久久婷婷国产麻豆91天堂,亚洲av无码a片在线观看

C語(yǔ)言

C++中const-cast與reinterpret-cast運算符的用

時(shí)間:2025-03-25 21:02:51 C語(yǔ)言 我要投稿
  • 相關(guān)推薦

C++中const-cast與reinterpret-cast運算符的用法

  C++中const_cast與reinterpret_cast運算符的用法,經(jīng)常被用于表達式中的類(lèi)型轉換,下面是小編分享的運算符的用法,一起來(lái)看一下吧。

  const_cast 運算符

  從類(lèi)中移除 const、volatile 和 __unaligned 特性。

  語(yǔ)法

  const_cast <

  type-id

  > (

  expression

  )

  備注

  指向任何對象類(lèi)型的指針或指向數據成員的指針可顯式轉換為完全相同的類(lèi)型(const、volatile 和 __unaligned 限定符除外)。對于指針和引用,結果將引用原始對象。對于指向數據成員的指針,結果將引用與指向數據成員的原始(未強制轉換)的指針相同的成員。根據引用對象的類(lèi)型,通過(guò)生成的指針、引用或指向數據成員的指針的寫(xiě)入操作可能產(chǎn)生未定義的行為。

  您不能使用 const_cast 運算符直接重寫(xiě)常量變量的常量狀態(tài)。

  const_cast 運算符將 null 指針值轉換為目標類(lèi)型的 null 指針值。

  // expre_const_cast_Operator.cpp

  // compile with: /EHsc

  #include <iostream>

  using namespace std;

  class CCTest {

  public:

  void setNumber( int );

  void printNumber() const;

  private:

  int number;

  };

  void CCTest::setNumber( int num ) { number = num; }

  void CCTest::printNumber() const {

  cout << " Before: " << number;

  const_cast< CCTest * >( this )->number--;

  cout << " After: " << number;

  }

  int main() {

  CCTest X;

  X.setNumber( 8 );

  X.printNumber();

  }

  在包含 const_cast 的行中,this 指針的數據類(lèi)型為 const CCTest *。 const_cast 運算符會(huì )將 this 指針的數據類(lèi)型更改為 CCTest *,以允許修改成員 number。強制轉換僅對其所在的語(yǔ)句中的其余部分持續。

  reinterpret_cast 運算符

  允許將任何指針轉換為任何其他指針類(lèi)型。也允許將任何整數類(lèi)型轉換為任何指針類(lèi)型以及反向轉換。

  語(yǔ)法

  reinterpret_cast < type-id > ( expression )

  備注

  濫用 reinterpret_cast 運算符可能很容易帶來(lái)風(fēng)險。除非所需轉換本身是低級別的,否則應使用其他強制轉換運算符之一。

  reinterpret_cast 運算符可用于 char* 到 int* 或 One_class* 到 Unrelated_class* 之類(lèi)的轉換,這本身并不安全。

  reinterpret_cast 的結果不能安全地用于除強制轉換回其原始類(lèi)型以外的任何用途。在最好的情況下,其他用途也是不可移植的。

  reinterpret_cast 運算符不能丟掉 const、volatile 或 __unaligned 特性。有關(guān)移除這些特性的詳細信息,請參閱 const_cast Operator。

  reinterpret_cast 運算符將 null 指針值轉換為目標類(lèi)型的 null 指針值。

  reinterpret_cast 的一個(gè)實(shí)際用途是在哈希函數中,即,通過(guò)讓兩個(gè)不同的值幾乎不以相同的索引結尾的方式將值映射到索引。

  #include <iostream>

  using namespace std;

  // Returns a hash code based on an address

  unsigned short Hash( void *p ) {

  unsigned int val = reinterpret_cast<unsigned int>( p );

  return ( unsigned short )( val ^ (val >> 16));

  }

  using namespace std;

  int main() {

  int a[20];

  for ( int i = 0; i < 20; i++ )

  cout << Hash( a + i ) << endl;

  }

  Output:

  64641

  64645

  64889

  64893

  64881

  64885

  64873

  64877

  64865

  64869

  64857

  64861

  64849

  64853

  64841

  64845

  64833

  64837

  64825

  64829

  reinterpret_cast 允許將指針視為整數類(lèi)型。結果隨后將按位移位并與自身進(jìn)行“異或”運算以生成唯一的索引(具有唯一性的概率非常高)。該索引隨后被標準 C 樣式強制轉換截斷為函數的返回類(lèi)型。


【C++中const-cast與reinterpret-cast運算符的用】相關(guān)文章:

c++運算符重載基礎知識詳解08-20

二級C++考點(diǎn)歸納:運算符重載08-27

Java中運算符的使用10-17

java中運算符的使用方法09-16

計算機二級C++重載雙目運算符概述09-20

c++ 中--declspec 的用法詳解08-13

C++中內聯(lián)函數的應用09-21

C++類(lèi)中的繼承實(shí)例詳解07-05

C++中輸入多組數據的方法09-24

一级日韩免费大片,亚洲一区二区三区高清,性欧美乱妇高清come,久久婷婷国产麻豆91天堂,亚洲av无码a片在线观看