MATLAB Object-Oriented Programming

(Joyce) #1
A delete method can access properties of the object being deleted. MATLAB does not
destroy these properties until after the delete methods for the class of the object and all
superclasses finish executing.

If a delete method creates new variables that contain a handle to the object being
deleted, those handles are invalid. After the delete method finishes execution, handles
to the deleted object in any variables in any workspace are invalid.

The isvalid method returns false for the handle object within the delete method
because object destruction begins when the method is called.

MATLAB calls delete methods in the inverse of the construction order. That is, MATLAB
invokes subclass delete methods before superclass delete methods.

If a superclass expects a property to be managed by subclasses, then the superclass
should not access that property in its delete method. For example, if a subclass uses an
inherited abstract property to store an object handle, then the subclass should destroy
this object in its delete method, but the superclass should not access that property in its
delete method.

Support Destruction of Partially Constructed Objects


Errors that occur while constructing an object can result in a call to delete before the
object is fully created. Therefore, class delete methods must be able to work with
partially constructed objects.

For example, the PartialObject class delete method determines if the Data property
is empty before accessing the data this property contains. If an error occurs while
assigning the constructor argument to the Name property, MATLAB passes the partially
constructed object to delete.

classdef PartialObject < handle
properties
% Restrict the Name property
% to a cell array
Name cell
Data
end
methods
function h = PartialObject(name)
if nargin > 0
h.Name = name;

7 Value or Handle Class — Which to Use

Free download pdf