Matlab Bug (?) Using Constant Properties in Subclass from Abstract superclass... breakpoints change behaviour

6 views (last 30 days)
Hi,
I have a situation where I have an Abstract superclass that declares it's subclasses must implement a Constant property.
eg
classdef parent
properties (Abstract, Constant)
name
end
end
and
classdef child1
properties (Constant)
name = 'ABC'
end
end
and
classdef child2
properties (Constant)
name = 'XYZ'
end
end
Now, I'm using these children classes as delegates for behaviour of an owning class. The owning class will have either a child1 or child2 class as one of its properties. I have a getter to retrieve the name. The idea is that a call like owner1.name returns the name assigned to whichever class happens to be the delegate, and should return 'ABC' or 'XYZ' depending on which class has been chosen.
classdef owner
properties
delegateObject
end
properties (Dependent)
name
end
methods
function n = get.name(obj)
n = obj.delegateObject.name;
end
end
end
If I have two owner objects, owner1 and owner2 (which have an instance of child1 and child2 as the delegateObjects, respectively) I get the following behaviour:
owner1.name
'ABC'
owner2.name
'ABC'
It's like it gets 'stuck' on one - whichever was accessed first.
What is weird is that just by putting a breakpoint into the getter function and continuing as soon as it's reached I get the 'correct' answer:
owner1.name
'ABC'
owner2.name
'XYZ'
This seems like a bug - surely a breakpoint that does nothing shouldn't change the outcome... Right?
  1 Comment
per isakson
per isakson on 9 Aug 2015
Whether this is a bug or not depends on what the documentation says.
However, it resembles a case, which is discussed at UndocumentedMatlab, Handle object as default class property value. See especially the comment by David Foti at April 10, 2015 at 2:42 pm.

Sign in to comment.

Answers (0)

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!