Calling the constructor when assigning custom classes as properties

4 views (last 30 days)
Assume if have two classes like:
classdef myclass1
properties
myprop1 myclass2
end
end
and
classdef myclass2
properties
myprop2 (1,1) double = 2
myprop3 (1,1) double = 3
end
end
When creating a myclass1 object, myprop1 will be an empty myclass2 type object. It seems like the constructor of myclass2 does not get called and thus the default values do not get assigned. Is there some "shortcut" to get myprop1 set up with the intendend default values? Or do I have to explicitly state a constructor for myclass1 and make it call the constructor of myclass2?
Edit: It is actually possible to call class constructors directly from within the properties section like so:
classdef myclass1
properties
myprop1 myclass2 = myclass2
end
end
This however leads to another problem, c. f. https://mathworks.com/help/matlab/matlab_oop/expressions-in-class-definitions.html In this way, the expression (constructor) is evaluated only once when the class is first instantiated and the resulting object is then assigned to all further instances on creation. This is tricky especially if myclass2 is a handle class since in this case, subsequent changes of myprop1 values in one myclass1 object will apply to ALL myclass1 objects.
Does that sink the ship or is there yet another way I am not aware of?
[SL: Removed extraneous : from the end of the hyperlink]

Accepted Answer

Matt J
Matt J on 12 Mar 2023
Edited: Matt J on 12 Mar 2023
subsequent changes of myprop1 values in one myclass1 object will apply to ALL myclass1 objects.
That is not true. You are free to overwrite the default myprop1 value with other myclass2 instances. Different instances will be independent of one another regardless of whether myclass2 is a handle or value class:
obj=myclass1;
obj.myprop1=myclass2;
Also, instead of assigning a default myprop1 value, you could write a constructor for myclass1 which assigns a fresh, independent myclass2 instance every time it is called:
classdef myclass1
properties
myprop1
end
methods
function obj=myclass1
obj.myprop1=myclass2;
end
end
end
  2 Comments
broken_arrow
broken_arrow on 12 Mar 2023
Thanks Matt. So it comes down to calling the myclass2 constructor from within the myclass1 constructor.
Regarding your remark on the assignment of a new object: Of course one can do that, but using the method I referred to will lead to the behavior I described. Just wanted to point it out because I think it's a bit counterintuitive (I didn't know about that behavior before).
Steven Lord
Steven Lord on 12 Mar 2023
See this documentation page for a little more information on some of the considerations you need to account for when creating objects whose properties contain other objects.

Sign in to comment.

More Answers (0)

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!