Is it possible to make Matlab to call a copy method of my class TMyClass (which is a handle calss) when an object of this class is assigned to a variable. In other words, I want my handle class to behave as a value class when copying it:
obj = TMyClass(); %// has method "copy", which returns a deep copy of the object
%// Now, if I write this ...
obj_copy = obj;
%// ... I want Matlab to do in fact this:
obj_copy = obj.copy; %//
As I understood, there is no way to override the = operator in Matlab. Is there any other workaround to do so?
Thanks!
dynamicpropsclass. But I don't really want to modify all the functions (compatibility issues), which work with the structure and assume copy-by-value semantic. - Oleghandleclass comes from the inheritance ofdynamicpropsbyTMyClass; is there a way to avoid this (I can only see the need for that parent if you need to add properties to an instance in other classes/functions aware ofTMyClass's nature throughaddprop)? - TroyHaskinMyStruct.field = some_value, which assumes thatMyStructis a copy of the original one passed as an argument. (2) Indeed, an inheritance fromdynamicpropsis required because of need to add/remove properties dynamically. The problem is that I want all functions to work with old structures as well. The simplest solution I came to so far is to add a checking to each function: if the argument is an object then create a copy of it. But maybe there is more elegant solution... - Oleg