Several questions about overload the 'copyElement' method in subclasses of 'matlab.mixin.Copyable'

17 visualizaciones (últimos 30 días)
The matlab.mixin.Copyable page describes the following information:
1、The copy method makes a shallow copy of the object
2、copyElement is a Protected Methods
3、MATLAB® does not call copy recursively on any handles contained in property values.
4、The copy method copies data without calling the class constructor or property set functions. It therefore produces no side effects.
There is four questions:
1、The copy method makes a shallow copy of the object.--------But in overload copyElement code use ’copy‘ to Make a deep copy of the DeepCp object ---this page Why?So, can the copy method perform deep copying ? Or not?
methods(Access = protected)
% Override copyElement method:
function cpObj = copyElement(obj)
% Make a shallow copy of all four properties
cpObj = copyElement@matlab.mixin.Copyable(obj);
% Make a deep copy of the DeepCp object
cpObj.DeepObj = copy(obj.DeepObj);
end
end
By running the following code, you will notice that as long as it's an object of the matlab.mixin.Copyable class, using copy will achieve deep copying. Therefore, is it not an incorrect conclusion to say, "The copy method makes a shallow copy of the object"?
classdef DeepCp < matlab.mixin.Copyable
properties
DpProp
end
methods
function obj = DeepCp(val)
obj.DpProp=val;
end
end
end
A=DeepCp(7);
B=copy(A);
A.DpProp=222;
B
2、copyElement is a Protected Methods ,--------So, is it necessary to declare Access = protected when overloading copyElement? Is it always required to do so? Do you have to redeclare all method attributes when overloading built-in methods in MATLAB?
3、MATLAB® does not call copy recursively on any handles contained in property values.--------Since the copy method is sealed, it cannot be overloaded or defined. So how can a situation with recursive calls to copy occur? For instance, in the case of get methods, when you define a get method within a class, it may reference the class's properties, creating a possibility of recursive calls to the get method. Therefore, it is explicitly stated that there should not be a recursive call situation in get methods.
4、The copy method copies data without calling the class constructor or property set functions. It therefore produces no side effects.---------What built-in methods call "the class constructor or property set functions"? What are the side effects of calling the class constructor or property set functions?

Respuestas (1)

TARUN
TARUN el 13 de Feb. de 2025 a las 10:01
Hi @fa wu,
I have answered all the 4 queries below from my understanding of the 'copyElement' method:
1. When the documentation says that "the copy method makes a shallow copy of the object," it means that, by default, MATLAB will copy the object itself, but not the objects it points to. If the object has properties that reference other objects (like handle objects), the copy method just copies the references, not the actual objects themselves.
However, if you want to customize how copying works, you can change the 'copyElement' method. This allows you to implement deep copying, where not only the main object is copied but also the objects it references.
In your example, you override 'copyElement' to make sure that the 'DeepObj' property gets a deep copy by calling copy on it directly.
In short, the copy method on its own doesn't do deep copying by default, but you can use 'copyElement' to customize it and make deep copies where needed.
2. The 'copyElement' method is a protected method by default, meaning it can only be accessed by the class itself and its subclasses. So, if you are overloading this method, it should maintain its protected access level, otherwise, you might run into access issues when calling 'copyElement'.
It is not always required to declare the Access attribute when overloading built-in methods in MATLAB. The default access level for methods and properties is public, but when dealing with protected methods (like 'copyElement'), you need to specify Access = protected to ensure that the method behaves correctly and has the correct visibility within the class hierarchy.
3. The copy method does not recursively call copy on handle properties because doing so could lead to infinite loops or undesired behaviour, especially if an object contains references to itself (directly or indirectly).
You need to explicitly handle the copying of these properties, as shown in your example with 'DeepObj'.
4. In copy method, no constructor or setter methods are called because it is simply copying the data, not reinitializing the object. This is why there are no side effects no validation, no property updates, and no event triggering happens during the copy.

Categorías

Más información sobre Customize Object Indexing en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by