Cannot access protected property?
Show older comments
Hi all,
The following simple test code (in 3 separate files of course...) reproduces my problem:
classdef top_class < handle
methods
function do_something(obj, fieldname)
disp(obj.(fieldname))
end
end
end
classdef derived_class < top_class
properties (Access = protected)
P
end
end
function test
clc
close all
h = derived_class;
h.do_something('P', pi)
disp('Done')
Error:
Getting the 'P' property of the 'derived_class' class is not allowed.
Error in top_class/do_something (line 4)
disp(obj.(fieldname))
The method 'do_something()' was set up this way such that users can perform some restricted operations on protected properties in all sorts of dervied classes, while the method also does a lot of things that have to do with the top-class class itself. It is important that the P-property is protected, the user is not allowed to directly modify it.
I would expect that the above structure is not a problem since P is protected, but do_something() is inherited from top_class and therefore also a method of derived_class. Why does the error occur, and more important: how can I solve this?
Thanks in advance for thinking with me,
Jeroen
Accepted Answer
More Answers (0)
Categories
Find more on Use Prebuilt MATLAB Interface to C++ Library in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!