delphi: you can only access protected identifiers from parent classes in your own “Self” scope, or when you are “friends” with your parent (so you are in the same unit)
Posted by jpluimers on 2019/09/11
An interesting question from a while back: [WayBack] delphi – Should a descendant class’ method’s variable that is identical to Self, have access to its ancestor’s protected methods? – Stack Overflow
In unit A:
TParent = class protected function DoSomething: TParent; end;
In unit B:
TChild = class(TParent) public procedure DoAnotherThing; end; implementation procedure TChild.DoAnotherThing; begin DoSomething.DoSomething end;
…
This won’t compile, throwing a
cannot access protected symbol TParent.DoSomething
The kicker here is that the error message makes you think you are operating in Self
context, but you are not as you are calling DoSomething.DoSomething
where only the first DoSomething
is in your Self
context, but the second .DoSomething
is in the context of any TParent
instance trying to access a public identifier.
Stefan Glienke posted [WayBack] a more elaborate answer explaining some workarounds.
–jeroen
Leave a comment