Not
Bu sayfaya erişim yetkilendirme gerektiriyor. Oturum açmayı veya dizinleri değiştirmeyi deneyebilirsiniz.
Bu sayfaya erişim yetkilendirme gerektiriyor. Dizinleri değiştirmeyi deneyebilirsiniz.
İşlem, safe_cast belirtilen ifadeyi belirtilen tür olarak döndürür. İşlem başarılı olmazsa bir InvalidCastExceptionoluşturur.
All Runtimes
(Bu dil özelliği için tüm çalışma zamanları için geçerli olan hiçbir açıklama yoktur.)
Syntax
[default]:: safe_cast< type-id >( expression )
Windows Runtime
Belirtilen ifadenin türünü değiştirmek için kullanın safe_cast . 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. Başvuru veya değer türünün tanıtıcısı, değer türü veya başvuruya veya değer türüne izleme başvurusu.
expression
Bir başvuruya veya değer türüne, değer türüne veya başvuruya veya değer türüne yönelik izleme başvurusuna yönelik tanıtıcıyı değerlendiren ifade.
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
Derleyici seçeneği: /ZW
Examples
Aşağıdaki kod örneği, Windows Çalışma Zamanı ile nasıl kullanılacağını safe_cast gösterir.
// 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
Ortak Dil Çalışma Zamanı
safe_cast bir ifadenin türünü değiştirir ve doğrulanabilir MSIL kodu oluşturur.
Syntax
[cli]:: safe_cast< type-id >( expression )
Parameters
type-id
Başvuru veya değer türünün tanıtıcısı, değer türü veya başvuruya veya değer türüne izleme başvurusu.
expression Bir başvuruya veya değer türüne, değer türüne veya başvuruya veya değer türüne yönelik izleme başvurusuna yönelik tanıtıcıyı değerlendiren ifade.
Remarks
The expression safe_cast<type-id>(expression) converts the operand expression to an object of type type-id.
Derleyici, çoğu yerde bir static_cast kabul safe_casteder. Ancak, safe_cast her zaman doğrulanabilir MSIL üretirken, doğrulanamayan static_cast MSIL üretebilir. Doğrulanabilir kod hakkında daha fazla bilgi için bkz. Saf ve Doğrulanabilir Kod (C++/CLI) ve Peverify.exe (PEVerify Aracı).
gibi static_cast, safe_cast kullanıcı tanımlı dönüştürmeleri çağırır.
For more information about casts, see Casting Operators.
safe_cast bir const_cast uygulamaz (atılamaz const).
safe_cast cli ad alanındadır. Daha fazla bilgi için bkz. Platform, varsayılan ve cli Ad Alanları.
hakkında safe_castdaha fazla bilgi için bkz:
Requirements
Derleyici seçeneği: /clr
Examples
Derleyicinin kabul static_cast etmediği ancak safe_cast kabul ettiği bir örnek, ilişkisiz arabirim türleri arasındaki atamalar içindir. ile safe_cast, derleyici dönüştürme hatası vermez ve atamanın mümkün olup olmadığını görmek için çalışma zamanında bir denetim gerçekleştirir.
// 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