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)
Ältere Kommentare anzeigen
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?
0 Kommentare
Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!