Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Why does property access work when subsref is overriden?

1 Ansicht (letzte 30 Tage)
Thurman Ye
Thurman Ye am 6 Jun. 2019
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
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?

Antworten (0)

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by