Cross-Talk between handle Class Objects?
11 ビュー (過去 30 日間)
古いコメントを表示
I've created an abstract super class MySuperClass which is itself derived from matlab.mixin.Copyable. This super class provides a setValue(row, column, value) and a corresponding getValue(row, column) function. From this super class I derived a sub class MyClass whose constructor requires two integers.
By ...
a = MyClass(1, 2);
b = MyClass(1, 2);
... I created two objects of that class. isa(a, 'handle') returns logical true - same for b - which confirms that both objects are handle objects.
As...
a == b
... returns (logical) 0, I know that these are independent handles, i.e., independent objects. Right after creation of the objects isequal(a, b) returns logical 1.
Now, something strange happens:
a.setValue(1, 1, 5); % Set first row, first column value of a to 5
b.setValue(1, 1, 3); % Set first row, first column value of b to 3
a.getValue(1, 1)
-> 3
So, there is some "cross talk" between these objects even though they are independent.
Can you please explain why? this happens and how? to get rid of that behaviour. ??? I'm lost.
I'm using Matlab R2016b (9.1.0.441655) on 64-bit Windows.
Kindly, Joschi
1 件のコメント
Varun Gunda
2017 年 4 月 6 日
Can you share the entire code? This is not the expected behavior. May be your code is missing something!
採用された回答
Adam
2017 年 4 月 6 日
This depends entirely on what your setValue and getValue functions do, but taking a flying guess...
If you declare a property that is a handle object and you actually create this within the property block, both objects will have the same object because property block initialisations are evaluated once per class not per object.
So if the thing that is having its value set is some handle class object stored in mySuperClass that you initialised in the property block then this behaviour would occur as they will share a reference to the same actual object.
その他の回答 (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!