Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net C++/CLI堆栈语义相当于C#和#x27;正在使用语句的现有对象?_.net_C++ Cli_Idisposable_Using_Finally - Fatal编程技术网

.net C++/CLI堆栈语义相当于C#和#x27;正在使用语句的现有对象?

.net C++/CLI堆栈语义相当于C#和#x27;正在使用语句的现有对象?,.net,c++-cli,idisposable,using,finally,.net,C++ Cli,Idisposable,Using,Finally,我知道C++/CLI相当于此C代码: 这是: { SomeClass x(foo); // ... } 但是,有没有一种类似简洁、简洁的表达方式: using (SomeClass x = SomeFunctionThatReturnsThat(foo)) { // ... } 或: ??我拥有的最接近的工作示例如下: SomeClass^ x = SomeFunctionThatReturnsThat(foo); try { // ... } finally

我知道C++/CLI相当于此C代码:

这是:

{
    SomeClass x(foo);
    // ...
}
但是,有没有一种类似简洁、简洁的表达方式:

using (SomeClass x = SomeFunctionThatReturnsThat(foo))
{
    // ...
}
或:

??我拥有的最接近的工作示例如下:

SomeClass^ x = SomeFunctionThatReturnsThat(foo);
try
{
    // ...
}
finally
{
    if (x != nullptr) { delete x; }
}
但这似乎不太好。

是托管类型的智能指针:

#include <msclr/auto_handle.h>

{
    msclr::auto_handle<SomeClass> x(SomeFunctionThatReturnsThat(foo));
    // ...
}

// or

SomeClass^ x = SomeFunctionThatReturnsThat(foo);
{
    msclr::auto_handle<SomeClass> y(x);
    // ...
}
#包括
{
msclr::auto_handle x(返回(foo)的函数);
// ...
}
//或
SomeClass^x=返回(foo)的函数;
{
msclr::自动_手柄y(x);
// ...
}
SomeClass^ x = SomeFunctionThatReturnsThat(foo);
try
{
    // ...
}
finally
{
    if (x != nullptr) { delete x; }
}
#include <msclr/auto_handle.h>

{
    msclr::auto_handle<SomeClass> x(SomeFunctionThatReturnsThat(foo));
    // ...
}

// or

SomeClass^ x = SomeFunctionThatReturnsThat(foo);
{
    msclr::auto_handle<SomeClass> y(x);
    // ...
}