[fpc-pascal] 2.1.1 new protected

Mattias Gaertner nc-gaertnma at netcologne.de
Fri Aug 11 13:58:36 CEST 2006


On Fri, 11 Aug 2006 13:42:49 +0200
Florian Klaempfl <florian at freepascal.org> wrote:

> Mattias Gaertner wrote:
> > Recently the behaviour of the 'protected' keyword has changed in fpc 2.1.1.
> > Now I wonder how can I fix the code, that depends on this.
> 
> Redesign :)

Of FPC? Naaah, too much work. ;)

Of my code: Yes, but how?
Looking at the Delphi sources, I see that Borland has made many such methods public, which resulted in Code using the VCL the wrong way and creating VCL version dependent conflicts. I want to avoid this, and the old protected allowed that. But now I can't find any good alternative.


> > I have some base classes, that defines methods to let derived classes interact.
> > These methods should not be accessed directly from the outside, so they are protected. But this does not work any longer. Of course I can safely typecast, but for aesthetic reasons it looks pretty bad coding style to typecast a class to the wrong class. For example:
> > 
> > -- unit1 -------------------------
> >   TMyClass = class
> >     FFlag: boolean;
> >   protected
> >     procedure InternalSetFlag(b: boolean);
> >   end;
> > 
> > -- unit2 -------------------------
> >   TDescendantClass1 = class(TMyClass)
> >   public
> >     procedure CallProtectedMethod(AnObject: TMyClass);
> >   end;
> > 
> >   TDescendantClass2 = class(TMyClass)
> >   end;
> > 
> > procedure TDescendantClass1.CallProtectedMethod(AnObject: TMyClass);
> > begin
> >   InternalSetFlag(true); // allowed
> >   AnObject.InternalSetFlag(true); // not allowed any longer
> >   TDescendantClass1(AnObject).InternalSetFlag(true); // allowed and works, but ugly
> 
> 
> Shouldn't be allowed either imo.

What's the alternative?


Mattias



More information about the fpc-pascal mailing list