[fpc-pascal] 2.1.1 new protected

Jonas Maebe jonas.maebe at elis.ugent.be
Fri Aug 11 13:52:07 CEST 2006


On 11 aug 2006, at 13:35, Mattias Gaertner wrote:

> 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.

Not only that, but you'll get a run time error when compiling with -CR.

> 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

I think that this shouldn't be allowed either, to be honest. What you  
seem to want is some sort of "friend" classes like in C++. I doubt  
this will be added any time soon though.


Jonas



More information about the fpc-pascal mailing list