C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c -cli – C/C++LI中的RAII大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经习惯了C RAII设施,我希望在C/C++LI中以正确的方式使用RAII托管代码. Herb SutterMicrosoft都告诉我这是最好的做法.

我有这样的事情:@H_772_3@

ref struct Managed
{
    // No default constructor
    Managed( /*...*/ ) { /*...*/ }
    ~Managed() { /* Important non-managed resource release here */ }
    // ...
};

ref struct Usesmanaged
{
    Managed^         m_;
    array<Managed^>^ a_;

    Usesmanaged( Managed^ m,array<Managed^>^ a ) : m_(m),a_(a) {}
    // ...
};

ref struct Creator
{
    Managed^         m_;
    array<Managed^>^ a_;
    Usesmanaged^     u_;

    Creator()
    {
        // Must allocate dynamically here,not in initializer list
        // because in my real code,I use "this" here for a callBACk.
        m_      = gcnew Managed( /*...*/ );
        a_      = gcnew array<Managed^>( 2 );
        a_[ 0 ] = gcnew Managed( /*...*/ );
        a_[ 1 ] = gcnew Managed( /*...*/ );
        u_      = gcnew Usesmanaged( m_,a_ );
    }
};

我想(1)自动资源销毁,所以我不必手动删除每个gcnew’ed对象,特别是在异常情况下; (2)安全清晰地共享对象的能力(绕过std :: auto_ptr等不符合条件); (3)能够让我的类被VB或C#消耗,并在对象超出范围时自动运行清理(例如,由于异常).@H_772_3@

在标准C中,我使用std :: shared_ptr和std :: vector或类似的工具来自动化RAII.在这里,我可以使用STL / CLI的向量,但没有shared_ptr等价物.我看到的唯一相关的C/C++LI智能指针是sparsely documented msclr::auto_handle,它类似于std :: auto_ptr,包括所有权转移语义,它与向量不兼容,尽管它们在数组中可以正常工作.@H_772_3@

什么是实现我的三个目标的正确的C/C++LI方式? (另请注意,我的主要C/C++LI类,上面的Creator,将由VB / C#使用.)@H_772_3@

[更新:在顶部添加了Herb Sutter和MS的链接,并添加了目标3(通过VB / C#消费).@H_772_3@

解决方法

你可以使用托管代码获得RAII:如果你有这个:
ref class A {
  ~A() { // implements/overrides the IDisposable::Dispose method
        // free managed and unmanaged resources here
   }
};

然后你可以这样做:@H_772_3@

void foo()
{
  A a(cons_args); // stack-like usage
  // use a ...
}

这将被有效地视为:@H_772_3@

void foo()
{
  try
  {
     A^ a_ = gcnew A(cons_args);
  }
  finally
  {
     a_->~A();
  }
}

大佬总结

以上是大佬教程为你收集整理的c -cli – C/C++LI中的RAII全部内容,希望文章能够帮你解决c -cli – C/C++LI中的RAII所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。