Initializing class property values outside of constructor
조회 수: 4 (최근 30일)
이전 댓글 표시
Here is a very simple class definition:
classdef DummyClass < handle
properties
myMap = containers.Map('KeyType','uint32','ValueType','uint32');
end
methods
end
end
Now, here is a very strange series of commands using this class:
>> d = DummyClass();
>> d.myMap(1) = 1;
>> clear;
>> d = DummyClass();
>> d
d =
DummyClass with properties:
myMap: [1x1 containers.Map]
>> d.myMap
ans =
Map with properties:
Count: 1
KeyType: uint32
ValueType: uint32
Why does the value of the myMap property persist across calls to clear !?!
I have noticed that when I move the initialization of the myMap property into the class constructor this does not happen. I already have several fixes for this 'problem' so I am not looking for a solution here - instead I am looking for an explanation.
Thank you.
댓글 수: 0
답변 (2개)
per isakson
2014년 2월 19일
편집: per isakson
2014년 2월 19일
Because Matlab behaves that way;-)
clear doesn't clear 'containers.Map', but clear classes does - according to inmem.
[~,~,C] = inmem; C
d = DummyClass();
clear
[~,~,C] = inmem; C
clear classes
[~,~,C] = inmem; C
returns
C =
'com.mathworks.mlwidgets.workspace.WhosInformation'
C =
'com.mathworks.mlwidgets.workspace.WhosInformation'
'containers.Map'
C =
Empty cell array: 0-by-1
.
Later
inmem
d = DummyClass();
d.myMap(1) = 117;
clear
d = DummyClass();
d
d.myMap(1)
returns
ans =
'workspacefunc'
'onCleanup'
'imformats'
d =
DummyClass with properties:
myMap: [1x1 containers.Map]
ans =
117
and
inmem
d = DummyClass();
d.myMap(1) = 117;
clear classes
d = DummyClass();
d
d.myMap(1)
returns
ans =
'workspacefunc'
'onCleanup'
'imformats'
d =
DummyClass with properties:
myMap: [0x1 containers.Map]
Error using containers.Map/subsref
The specified key is not present in this container.
댓글 수: 3
per isakson
2014년 2월 19일
편집: per isakson
2014년 2월 19일
Matlab is not open source and we cannot know WHY.
In the examples, which I added to my answer, clear does not behave as expected, i.e. as I interpret the documentation. However, clear classes does!
Whether this is an issue, a bug or a mistake in the documentation, I can only guess. (Or I have missed something in the documentation. That happens.)
per isakson
2014년 2월 19일
편집: per isakson
2014년 2월 21일
myMap is the default map of the class DummyClass. The value of myMap is a containers.Map. That map is share among all instances of DummyClass, whether or not it holds data. That seems to be by Design.
>> d = DummyClass
d =
DummyClass with properties:
myMap: [0x1 containers.Map]
>> d.myMap(1)=117;
>> e = DummyClass
e =
DummyClass with properties:
myMap: [1x1 containers.Map]
>> e.myMap(1)
ans =
117
[...]Default Values
Classes can define default values for properties. A default value is defined by the class and each instance of the class is assigned that default value. Default values can be used when there is a constant default that can be documented with the class definition. Default values can save space in MAT-Files because defaults are saved once per class and values of properties not differing from default values don’t need to be saved. Generally speaking default values should be literal values wherever possible so that it is clear what the default is.
.
Later
[...] Note: Evaluation of property default values occurs only when the value is first needed, and only once when MATLAB first initializes the class. MATLAB does not reevaluate the expression each time you create a class instance.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!