MATLAB Object-Oriented Programming

(Joyce) #1

To be a valid class destructor, the delete method:



  • Must define one, scalar input argument, which is an object of the class.

  • Must not define output arguments

  • Cannot be Sealed, Static, or Abstract


In addition, the delete method should not:



  • Throw errors, even if the object is invalid.

  • Create new handles to the object being destroyed

  • Call methods or access properties of subclasses


MATLAB does not call a noncompliant delete method when destroying objects of the
class. A delete method that is not a valid destructor shadows the handle base class
delete method. A noncompliant delete method can prevent the destruction of the
object by shadowing the handle class delete method.


Declare delete as an ordinary method:


methods
function delete(obj)
% obj is always scalar
...
end
end


delete Called Element-Wise on Array


MATLAB calls the delete method separately for each element in an array. Therefore, a
delete method is passed only one scalar argument with each invocation.


Calling delete on a deleted handle should not error and can take no action. This design
enables delete to work on object arrays containing a mix of valid and invalid objects.


Handle Object During delete Method Execution


Calling the delete method on an object always results in the destruction of the object.
The object is destroyed when the call to delete is made explicitly in MATLAB code or
when called by MATLAB because an object is no longer reachable from any workspace.
Once called, a delete method cannot abort or prevent object destruction.


Handle Class Destructor
Free download pdf