Overloading built in assign or '=' operator to display warning that you are copying a handle rather than an object

10 visualizzazioni (ultimi 30 giorni)
I have a class:
classdef myClass < dynamicprops & hgsetget
properties
myProp
end
methods
function newClass = myClass(input)
if isa(input,'myClass')
newClass=input;
elseif isa(input,'double')
newClass.myProp=input;
end
end
end
end
Which is a subclass of handle and so the objects are just handles. This means that the following occurs when using the '=' operator
>> firstClass=myClass(5)
firstClass =
myClass handle
Properties:
myProp: 5
Methods, Events, Superclasses
>> secondClass=firstClass
secondClass =
myClass handle
Properties:
myProp: 5
Methods, Events, Superclasses
>> secondClass.myProp=10
secondClass =
myClass handle
Properties:
myProp: 10
Methods, Events, Superclasses
>> firstClass
firstClass =
myClass handle
Properties:
myProp: 10
Methods, Events, Superclasses
Which is all intended and for a developer makes life good, however, this is a class used by many that may not realize what a handle is. I would like to have a warning come up letting the user know he is copying a handle rather than the object. My thought was to overload the '=' operator to display a warning then call the built-in. I have a method in my class for copying the object properties to a new object. If there are best practices for managing object handles, please point me in the right direction (this is mostly new to me). Is there any way of displaying a warning or even possibly have '=' copy the object rather than the handle?
Another semi-related question: dynamic properties do not show up in my variable editor even though they are not hidden, etc. How can I get them to do display?
Thanks in advance,
Drew

Risposte (1)

Daniel Shub
Daniel Shub il 11 Ago 2011
I have never used it, but I wonder if this might be related:
  1 Commento
Drew
Drew il 11 Ago 2011
Thanks for the reply Daniel. matlab.mixin.copyableclass implements useful copying functions for handle objects, especially for handle objects with handle properties. I may implement this in the future, but I have already written my own copy functions that I know work with dynamicprops. But I learned something from that link, so thanks! What I am looking for would prevent users from making mistakes while using my class.

Accedi per commentare.

Categorie

Scopri di più su Handle Classes in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by