MATLAB Object-Oriented Programming

(Joyce) #1

MATLAB Passes Handles by Value


A handle variable is a reference to an object. MATLAB passes this reference by value.


Handles do not behave like references in C++. If you pass an object handle to a function
and that function assigns a different object to that handle variable, the variable in the
caller is not affected. For example, suppose you define a function g2:


function y = g2(x)
x = SimpleHandleClass('green');
y = x;
end


Pass a handle object to g2:


obj = SimpleHandleClass('red');
y = g2(obj);
y.Color


ans =


green


obj.Color


ans =


red


The function overwrites the handle passed in as an argument, but does not overwrite the
object referred to by the handle. The original handle obj still references the original
object.


Static Properties


In MATLAB, classes can define constant properties, but not "static" properties in the
sense of other languages like C++. You cannot change constant properties from the initial
value specified in the class definition.


MATLAB has long-standing rules that variables always take precedence over the names of
functions and classes. Assignment statements introduce a variable if one does not exist.


Expressions of this form


Comparison of MATLAB and Other OO Languages
Free download pdf