Info

This question is closed. Reopen it to edit or answer.

Why does property access work when subsref is overriden?

1 view (last 30 days)
Below is a class which subclasses double and does no more than storing the extra instance variable y, initialized to 123.
classdef foo < double
properties
y;
end
methods
function obj = foo(x)
obj = obj@double(x);
obj.y = 123;
end
function x_ = subsref(obj, S)
switch S.type
case "()"
x = double(obj);
x_ = foo(x(S.subs{:}));
case "."
% no isprop check - propogate any errors upwards
% what exactly does the property access call?
x_ = obj.(S.subs);
return;
end
end
end
end
Example of usage:
a = foo([4 5 6]);
double(a) % displays 4 5 6
a.y % displays 123
I am confused on why x_ = obj.(S.subs) is permitted and works when S.subs == "y". I believe that obj.(S.subs) implicitly calls subsref(obj, substruct('.', S.subs), but such calls to subsref aren't allowed for builtin subclasses, e.g. double subclasses. What is actually going on here?

Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!