0

Assuming a member within a class has private access properties, i.e., GetAccess=private, if we use a "." type reference for that member in the overloaded subsref method, then the access property of that member becomes invalid at that point.

--------------------- So, is there a good way here to allow users to both retain access control for properties and overload indexing methods?

1 Answers1

0

If you want to overload () and/or {} indexing without affecting . indexing, you would start your subsref function with something like this:

if isequal(s(1).type, '.')
    [varargout{1:nargout}] = builtin('subsref',obj,s);
end

This simply calls the builtin (default) implementation of indexing if the first indexing operation is a ..

Of corse you can also make this part of a standard switch statement:

function varargout = subsref(obj,s)
   switch s(1).type
      case '.'
         [varargout{1:nargout}] = builtin('subsref',obj,s);
      case '()'
         ...
      case '{}'
         ...
      otherwise
         error('Not a valid indexing expression')

Reference: https://www.mathworks.com/help/matlab/matlab_oop/code-patterns-for-subsref-and-subsasgn-methods.html

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120