Cross-Talk between handle Class Objects?
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Joschi
el 30 de Mzo. de 2017
Comentada: Joschi
el 6 de Abr. de 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 comentario
Varun Gunda
el 6 de Abr. de 2017
Can you share the entire code? This is not the expected behavior. May be your code is missing something!
Respuesta aceptada
Adam
el 6 de Abr. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Whos en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!