Cross-Talk between handle Class Objects?

10 次查看(过去 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
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
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.
  1 个评论
Joschi
Joschi 2017-4-6
Hi Adam, Thanks a lot for your reply.
The reason for this "strange" behaviour was indeed a protected property which was incidently derived from "handle" as well.
Have a nice day, Joschi

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by