2006/8/11, Mattias Gaertner <[EMAIL PROTECTED]>:
On Fri, 11 Aug 2006 14:49:16 +0200
"Graeme Geldenhuys" <[EMAIL PROTECTED]> wrote:
> On 8/11/06, Michael Van Canneyt <[EMAIL PROTECTED]> wrote:
> > It seems normal to me that it is not just the current instance. You KNOW
this
> > method is there, in a descendent, so it seems logical that you know that it
> > exists for another instance as well.
>
> Coming from a Delphi background, I disagree here. As I understood the
> Delphi docs, protected methods are only visible in the current
> instance (decendant), and not instances passed in as parameters.
It does not matter how you got the instance, via parameter, variable or
function.
Only the class of the instance matters.
The screenshot says:
A protected member is visible [...] from any descendant class.
A protected method can be called from the definition of any method belonging to
a class that descends from the one where the protected member is declared.
Where do you read here anything about 'current instance'?
IMO the old 'Protected' was very easy to explain to people:
"Is the same as Private, except that the members of a Protected section are also
accessible to descendent types, even if they are implemented in other modules."
No exceptions. Pretty straight.
Yep this is it and I always understood it like that.
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.
I would make it work if you are actually in a descendent...? Because
for me typecasting AnObject to make it fit with a descendent and using
the protected method is a violation of the rule. If you are in a
descendent then you can call the protected method. Then a typecast
would not be required either sing you know the descendent.
So your initial example is acceptable. Here a note/comment I add and a
unit3 example to explain the simple point I raised.
-- unit2 -------------------------
procedure TDescendantClass1.CallProtectedMethod(AnObject: TMyClass);
begin
InternalSetFlag(true); // allowed
AnObject.InternalSetFlag(true); // allowed (why not?), you know the descendent?
TDescendantClass1(AnObject).InternalSetFlag(true); // allowed and
works, but ugly
end;
-- unit3 -------------------------
ADescendantClass1 = TDescendantClass1;
ADescendantClass2 = TDescendantClass2;
procedure Form1.TryAccessProtected(AnObject: TMyClass);
begin
AnObject.InternalSetFlag(true); // not allowed
TDescendantClass1(AnObject).InternalSetFlag(true); // not allowed
end;
-------------------------------
Best Regards.
--
Alexandre Leclerc
_______________________________________________
fpc-pascal maillist - fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal