请输入您要查询的百科知识:

 

词条 safe_cast
释义

safe_cast allows you to change the type of an expression and generate verifiable MSIL code.

Example

// 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

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2024/12/23 11:53:10