seems like a handle class nesting bug

조회 수: 17 (최근 30일)
Stuart MacGregor
Stuart MacGregor 2023년 5월 21일
댓글: Stuart MacGregor 2023년 5월 21일
classdef IqReader < handle
properties
fid=0
end
...
end
classdef InReader < handle
properties
reader=IqFileReader()
end
...
end
classdef T1Reader < InReader
...
end
classdef T2Reader < InReader
...
end
t1=T1Reader();
t2=T2Reader();
assert(t1.reader ~= t2.reader);
In the example above (when split into files as matlab requires), t1 and t2 get the same handle class object as a reader. e.g. changing t1.reader.fid changes t2.reader.fid
This does not seem right to me.
  댓글 수: 2
Stuart MacGregor
Stuart MacGregor 2023년 5월 21일
cut and paste bug in example: first class should be IqFileReader
Image Analyst
Image Analyst 2023년 5월 21일
Then edit it to correct the first post rather than post the correction as a comment.

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2023년 5월 21일
Not a bug. See the "Handle Objects as Default Property Values" section on this documentation page for an expanation and an alternate approach to give each instance of the class a separate handle object as its property.
  댓글 수: 2
Stuart MacGregor
Stuart MacGregor 2023년 5월 21일
The doc is unclear re my example however. It says different instances get the same handle-derived property: ok, but my example did not have different instances of the same class, it had two different derived classes, each with a single instance.
This is not good, and not documented. Call it an undocumented feature rather than a bug possibly.
Stuart MacGregor
Stuart MacGregor 2023년 5월 21일
However, it does seem good practice to initialise handle derived properties in the constructor - the behaviour if you don't is very unlikely to be desired, or expected.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Handle Classes에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by