You don't. C++ is not Pascal. The base destructor is called
automatically, always in the opposite order of the construction. In
other words, C++/CLI destructors work like C++ destructors from this aspect.
You don't have to (= shouldn't) inherit from IDisposable. It's
automatically done when a ref class has a destructor.
Sample code:
using namespace System;
ref class B
{
public:
~B() { Console::WriteLine("~B"); }
};
ref class D : public B
{
public:
~D() { Console::WriteLine("~D"); }
};
int main(array<System::String ^> ^args)
{
{ D d; }
Console::ReadKey(true);
return 0;
}
This outputs
~D
~B
as it would in native C++.
Note that if you allocate the class with gcnew, you must delete it, just
like in native C++:
D* d = gcnew D;
[...]
delete d;
You may want to use a finalizer in order to ensure that the native
resources are destroyed even if the caller forgets about delete. Also
note that in other languages programmers call the Dispose() method
instead of the delete operator, such as d.Dispose() in C#.
Tom
Lloyd Dupont wrote:
I create 2 ManagedC++ class inherithing from each other.
I can't see how do I call super destructor or ensure it's called :-/
template <class T>
public ref class CArray : System::IDisposable
{
protected:
~CArray()
{
if( ptr ) // I'm not sure of what IDisposable will do...
free( ptr );
ptr = nullptr;
}
private:
int length;
T* ptr;
};
template <class T>
public ref class CVector : CArray<T>
{
protected:
~CVector()
{
::~CArray();
}
};