c++-cli - C++ Ref 类不是 System::IDisposable 的成员;实现 IDisposable 时遇到麻烦

标签 c++-cli implementation idisposable

我想为我自己的对象类创建一个名为“Person”的全局向量。然而,编译器说

    error C2039: '{dtor}' : is not a member of 'System::IDisposable'
1>        c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::IDisposable'

所以我查看了如何实现 IDisposable(我现在知道它主要用于非托管资源)但似乎仍然无法通过以下方式实现它:

ref class Globals : System::IDisposable
{  
public: 
  static cliext::vector<Person^> person_data = gcnew cliext::vector<Person^>;
    void Dispose()
    {
         delete person_data;
    }
}; 

我得到的 2 个错误是:

error C2605: 'Dispose' : this method is reserved within a managed class
1>        did you intend to define a destructor?
error C3766: 'Globals' must provide an implementation for the interface method 'void System::IDisposable::Dispose(void)'
1>        c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::IDisposable::Dispose'

最佳答案

您不必显式派生自 IDisposable。关注MSDN doco , 使用以下模式:

ref class Globals
{
public:
    static cliext::vector<Person^> person_data = gcnew cliext::vector<Person^>;
    !Globals() // finalizer
    {
        delete person_data;
    {
protected:
    ~Globals() // destructor calls finalizer
    {
        this->!Globals();
    }
};

关于c++-cli - C++ Ref 类不是 System::IDisposable 的成员;实现 IDisposable 时遇到麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2217589/

相关文章:

c++-cli - 给 StreamWriter 一个 std::string 来写入一个 txt 文件

opengl - OpenGL 是如何在操作系统中实现的?

c# - 如果 Finalizer 调用 Dispose() ,您可以触发 "Disposing"事件吗?

c# - 微软的dispose模式结构的原因是什么?

c++ - 在 C++-CLI 中调用 IOS::exception(...) 会导致 EOF 从 std::getline(...) 中抛出 InteropServices.SEHException,为什么?

random - C++ random_shuffle() 它是如何工作的?

JavaScript - 如何注入(inject)不同的函数实现,例如警报(与 Web 和 Node.js 服务器端共享模块)

angularjs - 使用服务作为模型

android - 谁应该在什么时候处置 Intent ?

winforms - MessageBox::Show 中需要 C++/CLI 帮助