이 safe_cast 작업은 지정된 식을 지정된 형식으로 반환합니다. 작업이 성공 InvalidCastException하지 못하면 .
All Runtimes
(이 언어 기능에는 모든 런타임에 적용되는 설명이 없습니다.)
Syntax
[default]:: safe_cast< type-id >( expression )
Windows Runtime
지정된 식의 형식을 변경하는 데 사용합니다 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. 참조 또는 값 형식에 대한 핸들, 값 형식 또는 참조 또는 값 형식에 대한 추적 참조입니다.
expression
참조 또는 값 형식에 대한 핸들, 값 형식 또는 참조 또는 값 형식에 대한 추적 참조로 계산되는 식입니다.
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
컴파일러 옵션: /ZW
Examples
다음 코드 예제에서는 Windows 런타임과 함께 사용하는 safe_cast 방법을 보여 줍니다.
// 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
공용 언어 런타임
safe_cast 식의 형식을 변경하고 확인 가능한 MSIL 코드를 생성합니다.
Syntax
[cli]:: safe_cast< type-id >( expression )
Parameters
type-id
참조 또는 값 형식에 대한 핸들, 값 형식 또는 참조 또는 값 형식에 대한 추적 참조입니다.
expression 참조 또는 값 형식, 값 형식 또는 참조 또는 값 형식에 대한 추적 참조에 대한 핸들로 계산되는 식입니다.
Remarks
The expression safe_cast<type-id>(expression) converts the operand expression to an object of type type-id.
컴파일러는 대부분의 위치에서 을 수락 static_cast 합니다.safe_cast 그러나 safe_cast 항상 확인 가능한 MSIL을 생성하는 반면 static_cast , 확인할 수 없는 MSIL을 생성할 수 있습니다. 확인 가능한 코드에 대한 자세한 내용은 순수 및 확인 가능한 코드(C++/CLI) 및 Peverify.exe (PEVerify 도구)를 참조하세요.
safe_cast 마찬가지로 static_cast사용자 정의 변환을 호출합니다.
For more information about casts, see Casting Operators.
safe_cast는 (캐스팅)const을 const_cast 적용하지 않습니다.
safe_cast 은 cli 네임스페이스에 있습니다. 자세한 내용은 플랫폼, 기본값 및 cli 네임스페이스를 참조하세요.
자세한 safe_cast내용은 다음을 참조하세요.
Requirements
컴파일러 옵션: /clr
Examples
컴파일러가 a를 허용하지 static_cast 않지만 수락 safe_cast 하는 한 가지 예는 관련 없는 인터페이스 형식 간의 캐스트에 대한 것입니다. 컴파일 safe_cast러는 변환 오류를 실행하지 않고 런타임에 검사를 수행하여 캐스트가 가능한지 확인합니다.
// 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