Finalizers and C ++ / CLI Operators

In the following example, I get:

error C2300: 'UnmanagedClass' : class does not have a finalizer called '!SmartPointer' 

If I delete the operator->, this error will disappear. Can anyone explain why this is happening?

 // Unmanaged class. class UnmanagedClass { }; public ref class SmartPointer { public: SmartPointer(UnmanagedClass* u) : m_ptr(u) { } ~SmartPointer() { this->!SmartPointer(); } !SmartPointer() { delete m_ptr; } // This line triggers C2300. UnmanagedClass* operator->() { return m_ptr; } }; int main() { SmartPointer^ s = gcnew SmartPointer(new UnmanagedClass); } 
+4
source share
1 answer

You override the operator β†’, so when you do:

 ~SmartPointer() { this->!SmartPointer(); } 

You really call

 ~SmartPointer() { m_ptr->!SmartPointer(); } 

I believe that you can get around this by doing this, though:

 ~SmartPointer() { (*this).!SmartPointer(); } 
+5
source

All Articles