[fpc-pascal] 2.1.1 new protected
Mattias Gaertner
nc-gaertnma at netcologne.de
Fri Aug 11 16:14:24 CEST 2006
On Fri, 11 Aug 2006 15:45:00 +0200
"Graeme Geldenhuys" <graemeg.lists at gmail.com> wrote:
> On 8/11/06, Mattias Gaertner <nc-gaertnma at netcologne.de> wrote:
> > Where do you read here anything about 'current instance'?
>
> Maybe not in that snippet of text, but I read it somewhere... ;-)
:)
> > And it was very Delphi compatible. I mean, how many Delphians noticed the difference at all? It didn't hurt and gave more flexibility. So, I second Michael's proposal to at least keep it in mode objfpc.
>
>
> Maybe it is only me, but I am used to the Delphi way. It made you
> think twice about accessing methods deep in a hierachy (normally
> hidden). I do agree it will make it easier to access protected
> methods, but not so sure if that is a good thing.
It is has advantages and there is no good alternative.
The only disadvantage I see, is that code written with FPC would not work with this commercial third party compiler. That's why mode Delphi should use the new protected. But why mode objfpc?
> I created a few classes as shown below, but doing this made me think
> about it first, before blindly accessing methods.
You can only access public methods *blindingly*.
Protected methods can only be used by descendants. That's why Martin and I meant with the 'public' hell of the VCL.
> { Create a friend class so we can access the protected AddAction method. }
> TActionListFriend = class(TActionList)
> end;
>
> And used it as follows....
>
> aOK := TAction.Create(self);
> aOK.Caption := csOK;
> aOK.OnExecute := @aOKExecute;
> { Must add to list otherwise it doesn't execute }
> TActionListFriend(FActionList).AddAction(aOK);
>
>
> This reminds me, Mattias, must TActionList.AddAction be protected? I
> had to do the above to create actionlists via code and get the actions
> to execute correctly.
Thanks for the good example. With the protected method you can easily do dangerous things, like adding an action twice or forgetting to remove it. With the 'protected' AddAction you could do such things only with dirty tricks like the above. The normal way is:
aOK.ActionList:=FActionList;
This is not only shorter, but also safe.
Mattias
More information about the fpc-pascal
mailing list