Overloading built in assign or '=' operator to display warning that you are copying a handle rather than an object
10 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
See Also
Categories
Find more on Handle Classes in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!