我想为我自己的对象类创建一个名为“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/