Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Operasi safe_cast mengembalikan ekspresi yang ditentukan sebagai jenis yang ditentukan. Jika operasi tidak berhasil, operasi akan melempar InvalidCastException.
All Runtimes
(Tidak ada keterangan untuk fitur bahasa ini yang berlaku untuk semua runtime.)
Syntax
[default]:: safe_cast< type-id >( expression )
Windows Runtime
Gunakan safe_cast untuk mengubah jenis ekspresi yang ditentukan. If you expect a variable or parameter to be convertible to a certain type, use safe_cast without a try-catch block to detect programming errors during development. For more information, see Casting (C++/CX).
Syntax
[default]:: safe_cast< type-id >( expression )
Parameters
type-id
The type to convert expression to. Handel ke jenis referensi atau nilai, jenis nilai, atau referensi pelacakan ke referensi atau jenis nilai.
expression
Ekspresi yang mengevaluasi ke handel ke jenis referensi atau nilai, jenis nilai, atau referensi pelacakan ke jenis referensi atau nilai.
Remarks
safe_cast throws InvalidCastException if it can't convert expression to the type specified by type-id. To catch InvalidCastException, specify the /EH (Exception Handling Model) compiler option, and use a try/catch statement.
Requirements
Opsi pengkompilasi: /ZW
Examples
Contoh kode berikut menunjukkan cara menggunakan safe_cast dengan Windows Runtime.
// safe_cast_ZW.cpp
// compile with: /ZW /EHsc
using namespace default;
using namespace Platform;
interface class I1 {};
interface class I2 {};
interface class I3 {};
ref class X : public I1, public I2 {};
int main(Array<String^>^ args) {
I1^ i1 = ref new X;
I2^ i2 = safe_cast<I2^>(i1); // OK, I1 and I2 have common type: X
// I2^ i3 = static_cast<I2^>(i1); C2440 use safe_cast instead
try {
I3^ i4 = safe_cast<I3^>(i1); // Fails because i1 is not derived from I3.
}
catch(InvalidCastException^ ic) {
wprintf(L"Caught expected exception: %s\n", ic->Message);
}
}
Caught expected exception: InvalidCastException
Runtime Bahasa Umum
safe_cast mengubah jenis ekspresi dan menghasilkan kode MSIL yang dapat diverifikasi.
Syntax
[cli]:: safe_cast< type-id >( expression )
Parameters
type-id
Handel ke jenis referensi atau nilai, jenis nilai, atau referensi pelacakan ke referensi atau jenis nilai.
expression Ekspresi yang mengevaluasi ke handel ke jenis referensi atau nilai, jenis nilai, atau referensi pelacakan ke jenis referensi atau nilai.
Remarks
The expression safe_cast<type-id>(expression) converts the operand expression to an object of type type-id.
Pengkompilasi menerima static_cast di sebagian besar tempat bahwa ia menerima safe_cast. Namun, safe_cast selalu menghasilkan MSIL yang dapat diverifikasi, sedangkan mungkin menghasilkan MSIL yang static_cast tidak dapat diverifikasi. Untuk informasi selengkapnya tentang kode yang dapat diverifikasi, lihat Kode Murni dan Dapat Diverifikasi (C++/CLI) dan Peverify.exe (ALAT PEVerify).
Seperti static_cast, safe_cast memanggil konversi yang ditentukan pengguna.
For more information about casts, see Casting Operators.
safe_cast tidak menerapkan const_cast (dibuang const).
safe_cast berada di namespace cli. Untuk informasi selengkapnya, lihat Namespace Platform, default, dan cli.
Untuk informasi selengkapnya tentang safe_cast, lihat:
Requirements
Opsi pengkompilasi: /clr
Examples
Salah satu contoh di mana pengkompilasi tidak menerima static_cast tetapi menerima safe_cast adalah untuk cast antara jenis antarmuka yang tidak terkait. Dengan safe_cast, pengkompilasi tidak mengeluarkan kesalahan konversi dan melakukan pemeriksaan pada runtime untuk melihat apakah pemeran dimungkinkan.
// safe_cast.cpp
// compile with: /clr
using namespace System;
interface class I1 {};
interface class I2 {};
interface class I3 {};
ref class X : public I1, public I2 {};
int main() {
I1^ i1 = gcnew X;
I2^ i2 = safe_cast<I2^>(i1); // OK, I1 and I2 have common type: X
// I2^ i3 = static_cast<I2^>(i1); C2440 use safe_cast instead
try {
I3^ i4 = safe_cast<I3^>(i1); // fail at runtime, no common type
}
catch(InvalidCastException^) {
Console::WriteLine("Caught expected exception");
}
}
Caught expected exception