.net C++/CLI堆栈语义相当于C#和#x27;正在使用语句的现有对象?
我知道C++/CLI相当于此C代码: 这是:.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
{
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);
// ...
}