How can I declare class member data public?

3 views (last 30 days)
I would like to make class member data accessible from non-member functions through subscript (dot) notation, as in C++.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 4 Nov 2020
Edited: MathWorks Support Team on 4 Nov 2020
This behavior is supported by implementing the SUBSREF and SUBSASGN members for an object. These are invoked to handle the following cases:
A=MyClass(initial)
out = A(I)% Invoke out=A.SUBSREF(A,S)
out = A{I}
out = A.field
A(I) = in% Invoke A.SUBSASN(A,S,in)
A{I} = in
A.field = in
where S is a structure that describes the type of indexing.
More information about the SUBSASGN and SUBSREF commands can be found here:
https://www.mathworks.com/help/matlab/ref/subsasgn.html
  1 Comment
Steven Lord
Steven Lord on 4 Nov 2020
If the class does not overload indexing, see the "Access to Property Values" section on this documentation page. By default, class properties have public Access and so anyone with access to the object can get or set the property value.

Sign in to comment.

More Answers (0)

Categories

Find more on Argument Definitions in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!