MATLAB Object-Oriented Programming

(Joyce) #1

Determining Equality of Objects


Equality for value objects means that the objects are of the same class and have the same
state.

Equality for handle objects means that the handle variables refer to the same object. You
also can identify handle variables that refer to different objects of the same class that
have the same state.

Equality of Value Objects

To determine if value objects are the same size and their contents are of equal value, use
isequal. For example, use the previously defined NumValue class to create two
instances and test for equality:

a = NumValue;
b = NumValue;
isequal(a,b)

ans =

1

a and b are independent and therefore are not the same object. However each represents
the same value.

If you change the value represented by a value object, the objects are no longer equal.

a = NumValue;
b = NumValue;
b.Number = 7;
isequal(a,b)

ans =

0

Value classes do not have a default eq method to implement the == operation.

Equality of Handle Objects

Handle objects inherit an eq method from the handle base class. You can use == and
isequal to test for two different relationships among handle objects:

7 Value or Handle Class — Which to Use

Free download pdf