Cross-Talk between handle Class Objects?

10 Ansichten (letzte 30 Tage)
Joschi
Joschi am 30 Mär. 2017
Kommentiert: Joschi am 6 Apr. 2017
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 Kommentar
Varun Gunda
Varun Gunda am 6 Apr. 2017
Can you share the entire code? This is not the expected behavior. May be your code is missing something!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam
Adam am 6 Apr. 2017
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 Kommentar
Joschi
Joschi am 6 Apr. 2017
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by