C++ での演算子のキャスト

キャスト演算子は、C++ で型キャストに使用されます。これらは、あるデータ型を別のデータ型に変換するために使用されます。 C++ は 4 種類のキャストをサポートしています。

  1. static_cast
  2. ダイナミックキャスト
  3. const_cast
  4. 再解釈_キャスト

1.静的キャスト

static_cast 演算子は、C++ で最も一般的に使用されるキャスト演算子です。これはコンパイル時の型変換を実行し、主にコンパイラによって安全であるとみなされる明示的な変換に使用されます。

static_cast の構文

  static_cast    <  new_type>(表現);> 

どこ、

  • 表現: 変換するデータ。
  • 新しいタイプ: ご希望の表現方法

static_cast は、同じ継承階層内の数値型やポインターなど、関連する型間の変換に使用できます。

static_castの例

C++
// C++ program to illustrate the static_cast #include  #include  using namespace std; int main() { int num = 10; // converting int to double double numDouble = static_cast (数字); // 印刷データ型 cout < < typeid(num).name()  < < endl; // typecasting cout  < < typeid(static_cast (番号)).name() < < endl; // printing double type t cout  < < typeid(numDouble).name()  < < endl; return 0; } 

出力
i d d 

この例では、 タイプ情報 ライブラリを使用できるようにする タイプID() データ型をチェックする関数。整数変数「num」を定義し、static_cast を使用してそれを double に変換しました。その後、変数のデータ型を出力して渡します。 static_cast(数値) タイプID() データ型をチェックする関数。出力 i、d、d が出力されることがわかります。 '私' を示します 整数 そして 「だ」 を示します ダブル

2. ダイナミックキャスト

ダイナミックキャスト 演算子は主にダウンキャスト (基本クラスのポインター/参照を派生クラスに変換する) を実行するために使用されます。実行時チェックを実行して変換の有効性を検証することで、型の安全性を確保します。

Dynamic_cast の構文

  dynamic_cast    <  new_type>(表現);> 

変換できない場合は、 ダイナミックキャスト を返します ヌルポインタ (ポインタ変換の場合) または、 bad_cast 例外 (参照変換用)。

ダイナミックキャストの例

C++
// C++ program to illustrate the dynamic_cast #include  using namespace std; // Base Class class Animal { public: virtual void speak() const { cout  < < 'Animal speaks.'  < < endl; } }; // Derived Class class Dog : public Animal { public: void speak() const override { cout  < < 'Dog barks.'  < < endl; } }; // Derived Class class Cat : public Animal { public: void speak() const override { cout  < < 'Cat meows.'  < < endl; } }; int main() { // base class pointer to derived class object Animal* animalPtr = new Dog(); // downcasting Dog* dogPtr = dynamic_cast (animalPtr); // 型キャストが成功したかどうかをチェックする l if (dogPtr) { DogPtr->speak(); } else { cout < < 'Failed to cast to Dog.'  < < endl; } // typecasting to other dervied class Cat* catPtr = dynamic_cast (animalPtr); if (catPtr) { catPtr->speak(); } else { cout < < 'Failed to cast to Cat.'  < < endl; } delete animalPtr; return 0; } 

出力
Dog barks. Failed to cast to Cat. 

説明: 出力の最初の行が印刷されるのは、 「animalPtr」 '動物' 型が正常にキャストされました '犬' タイプと 話す() Dog クラスの関数が呼び出されますが、 '動物' に入力します '猫' タイプが失敗したため 「animalPtr」 を指す '犬' したがって、型キャストが安全ではないため、動的キャストは失敗します。

3.const_cast

const_cast 演算子は、変数の const または volatile 修飾子を変更するために使用されます。これにより、プログラマはオブジェクトの不変性を一時的に削除し、変更を加えることができます。 const オブジェクトを変更すると未定義の動作が発生する可能性があるため、const_cast を使用する場合は注意が必要です。

const_cast の構文

  const_cast    <  new_type>(表現);> 

const_castの例

C++
// C++ program to illustrate the const_cast #include  using namespace std; int main() { const int number = 5; // Pointer to a const int const int* ptr = &number; // int* nonConstPtr = ptr; if we use this // instead of without using const_cast // we will get error of invalid conversion int* nonConstPtr = const_cast (ptr); *nonConstPtr = 10;コート < < 'Modified number: '  < < *nonConstPtr; return 0; } 

出力
Modified number: 10 

上の例では、の値を変更しました。 定数型 ポインタの修飾子を次のように変更します。 定数 非定数 そして変更された値を出力します。

4.再解釈キャスト

reinterpret_cast 演算子 ポインタを他のタイプのポインタに変換するために使用されます。変換されたポインタが同じ型であるかどうかのチェックは行いません。

reinterpret_cast の構文

  reinterpret_cast    <  new_type>(表現);> 

C++
// C++ program to illustrate the reinterpret_cast #include  using namespace std; int main() { int number = 10; // Store the address of number in numberPointer int* numberPointer = &number; // Reinterpreting the pointer as a char pointer char* charPointer = reinterpret_cast (数値ポインタ); // メモリアドレスと値の出力 cout < < 'Integer Address: '  < < numberPointer  < < endl; cout  < < 'Char Address: '  < < reinterpret_cast (charPointer) < < endl; return 0; } 

出力
Integer Address: 0x7fff64789f1c Char Address: 0x7fff64789f1c 

上の例では、int 変数を定義しました。 '番号' そして、「番号」のアドレスを 「数値ポインタ」 int 型の 「数値ポインタ」 int 型の値を char ポインタに格納してから、 「charPointer」 変数。 numberPointer と charPointer の両方のアドレスが出力されたことを確認します。 「charPointer」に格納されているアドレスを出力するには 再解釈_キャスト これは、C++ の型チェック メカニズムをバイパスし、型固有の解釈を行わずにポインターを汎用メモリ アドレスとして出力できるようにするために使用されます。

注記: const_cast と reinterpret_cast は、さまざまな種類のエラーに対して脆弱であるため、通常は推奨されません。