Reputation: 4855
I have defined a simple class called MySettings
, which internally holds for others settings defined in a class called MySubSettings
:
%% --- Definition for MySubSettings
classdef MySubSettings < handle
properties
Fifi = 666;
end
end
%% --- Definition for MySettings
classdef MySettings < handle
properties
Riri = 42;
Subs = MySubSettings();
end
end
The first time I instantiate MySettings
with s = MySettings();
everything is ok (Property Riri
is initialized to 42
and Subs
is an instance of MySubSettings
with property Fifi
initialized to 666
) ... So far so good ...
Now if I modify property values and reassign s
to new instance like this:
s.Riri = 0;
s.Subs.Fifi = 0;
s = MySettings();
Then inspecting new values for properties in new s
instance ... I have s.Riri
which is equal to 42
(as expected) ... but s.Subs.Fifi
is still equal to 0
where I would have expected it to be reinitialized to 666
(???)
Why only Riri
is initialized ? Am I doing something wrong or is it a bug ?
Note: I have the issue with R2013b and R2014b.
Upvotes: 2
Views: 51
Reputation: 4855
My misunderstanding ...
Values assigned to properties in class definition are not initial values (like in C# for instance) BUT default values and are no longer re-evaluated after the class is first used.
In my example, as MySubSettings
is a handle class, any value assigned to it becomes the new default for next instances.
This default value concept is a bit confusing ... but ok, it was my mistake ... sorry.
Upvotes: 3