oop: how to switch between passing by reference and value
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi all,
i've a class that inherits the "handle" class, objects therefore always passed by reference. But now I want to copy an array of objects of this class such that i can modify the original and the copy seperately. Is there any easy way to be able to decide between copying and object and passing it by reference?
Thank you!
0 Kommentare
Akzeptierte Antwort
Daniel Shub
am 25 Apr. 2013
The documentation is pretty sparse, but also there: http://www.mathworks.co.uk/help/matlab/matlab_prog/copying-objects.html#brultwv
Copy Method for Handle Classes
Handle classes can derive copy functionality from the matlab.mixin.Copyable class. Class designers should investigate the use of this class in their class hierarchy design
0 Kommentare
Weitere Antworten (1)
Matt J
am 25 Apr. 2013
Bearbeitet: Matt J
am 25 Apr. 2013
When you want an independent copy of a handle object, call the constructor to create a new instance of the class. Then, if necessary, copy the data over one property at a time. You could write a class method e.g., called clone(), to automate this for you.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Software Development Tools finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!