Cast オペレーターは、 単項演算子 これにより、あるデータ型が別のデータ型に強制的に変換されます。
Linuxホスト
C++ は 4 種類のキャストをサポートしています。
- 静的キャスト
- ダイナミックキャスト
- 定数キャスト
- キャストの再解釈
この記事では、static_cast について詳しく説明することに重点を置きます。
静的キャスト
これは、使用できる最も単純なタイプのキャストです。それは コンパイル時のキャスト 。これは型間の暗黙的な変換 (int から float、または void* へのポインタなど) のようなことを行い、明示的な変換関数を呼び出すこともできます。
static_cast の構文
static_cast < dest_type>(ソース);>>static_cast の戻り値は次のようになります。 dest_type。
static_castの例
以下は、static_cast を実装するための C++ プログラムです。
C++
// C++ Program to demonstrate> // static_cast> #include> using> namespace> std;> // Driver code> int> main()> {> >float> f = 3.5;> >// Implicit type case> >// float to int> >int> a = f;> >cout <<>'The Value of a: '> << a;> >// using static_cast for float to int> >int> b =>static_cast><>int>>(f);>> '
The Value of b: '> << b;> }> |
>出力
The Value of a: 3 The Value of b: 3>
さまざまなシナリオでの static_cast の動作
1. プリミティブ データ型ポインターの static_cast:
次に、上記のコードにいくつかの変更を加えてみましょう。
C++
// C++ Program to demonstrate> // static_cast char* to int*> #include> using> namespace> std;> // Driver code> int> main()> {> >int> a = 10;> >char> c =>'a'>;> > >// Pass at compile time,> >// may fail at run time> >int>* q = (>int>*)&c;> >int>* p =>static_cast><>int>*>(&c);>> 0;> }> |
>
>
出力
error: invalid 'static_cast' from type 'int*' to type 'char*'>
説明: これは、特定のオブジェクト ポインタを別のオブジェクト ポインタに型キャストできると考えていても、それは違法であるため、static_cast ではそれが許可されないことを意味します。
Javaで文字列を整数に変換する
2. ユーザー定義の変換演算子を使用したオブジェクトの変換
static_cast は、クラスの変換演算子が定義されている場合、それを呼び出すことができます。オブジェクトとクラス間の変換の別の例を見てみましょう。
例:
C++
// C++ Program to cast> // class object to string> // object> #include> #include> using> namespace> std;> // new class> class> integer {> >int> x;> public>:> >// constructor> >integer(>int> x_in = 0)> >: x{ x_in }> >{> >cout <<>'Constructor Called'> << endl;> >}> >// user defined conversion operator to string type> >operator string()> >{> >cout <<>'Conversion Operator Called'> << endl;> >return> to_string(x);> >}> };> // Driver code> int> main()> {> >integer obj(3);> >string str = obj;> >obj = 20;> >// using static_cast for typecasting> >string str2 =>static_cast>(obj);> >obj =>static_cast>(30);> >return> 0;> }> |
>
>出力
Constructor Called Conversion Operator Called Constructor Called Conversion Operator Called Constructor Called>
説明: 上記の出力を 1 行ずつ理解してみましょう。
- いつ オブジェクト が作成されると、コンストラクターが呼び出されます。この場合、これは変換コンストラクターでもあります (C++14 ではルールが少し変更されています)。
- 作成するとき str のうち オブジェクト 、Conversion 演算子を定義しているため、コンパイラはエラーをスローしません。
- 作るときは オブジェクト = 20 、実際には変換コンストラクターを呼び出しています。
- 作るときは str2 のうち static_cast 、文字列とよく似ています str = オブジェクト ;ただし、厳密な型チェックが行われます。
- 書くとき obj = static_cast (30) 、30 を に変換すると、 整数 static_cast を使用します。
3. C++ での継承のための static_cast
static_cast は、継承の場合にアップキャストとダウンキャストの両方を提供できます。次の例は、アップキャストの場合の static_cast の使用法を示しています。
例:
C++
ループバッシュ用
// C++ Program to demonstrate> // static_cast in inheritance> #include> using> namespace> std;> class> Base> {};> class> Derived :>public> Base> {};> // Driver code> int> main()> {> >Derived d1;> > >// Implicit cast allowed> >Base* b1 = (Base*)(&d1);> > >// upcasting using static_cast> >Base* b2 =>static_cast>(&d1);> >return> 0;> }> |
文字列とJava
>
>
説明: 上記のコードはエラーなしでコンパイルされます。
- d1 のアドレスを取得して明示的に Base にキャストし、b1 に格納しました。
- d1 のアドレスを取得し、static_cast を使用してそれを Base にキャストし、b2 に格納しました。
上記の例では、基本クラスを public として継承しました。それをプライベートとして継承するとどうなるでしょうか?以下の例は、次のことを示しています。
例:
C++
// C++ program to demonstrate> // static_cast in case of> // private inheritance> #include> using> namespace> std;> class> Base> {};> class> Derived:>private> Base> {> >// Inherited private/protected> >// not public> };> // Driver code> int> main()> {> >Derived d1;> > >// Implicit type cast allowed> >Base* b1 = (Base*)(&d1);> > >// static_cast not allowed> >Base* b2 =>static_cast>(&d1);> >return> 0;> }> |
>
>
コンパイル時エラー:
[Error] 'Base' is an inaccessible base of 'Derived'>
説明: 上記のコードは、 コンパイルしない たとえそれを継承したとしても 保護された 。
したがって、継承の場合に static_cast を使用するには、基本クラスがアクセス可能で、仮想的でなく、明確でなければなりません。
4. static_cast から Void ポインタへのキャスト、および Void ポインタからのキャスト
static_cast 演算子を使用すると、任意のポインター型から void ポインターへのキャスト、またはその逆のキャストが可能になります。
例:
C++
// C++ program to demonstrate> // static_cast to cast 'to and> // from' the void pointer> #include> using> namespace> std;> // Driver code> int> main()> {> >int> i = 10;> >void>* v =>static_cast><>void>*>(&i);>> * ip =>static_cast><>int>*>(v);>> >return> 0;> }> |
>
スピーカーとは何ですか
>出力
10>