On Fri, 11 Aug 2006, Florian Klaempfl wrote:

Michael Van Canneyt wrote:


On Fri, 11 Aug 2006, Florian Klaempfl 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 :)


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.

Why not ? In a descendent class you 'know' the protected method.

But only of the current instance, no?

Well, nothing is said about this in the manuals; For me this is up to 
discussion.
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.

Personally, I think we should allow use of protected methods in a descendent, also for other instances. In the above example: AnObject.InternalSetFlag(true); Should work. But since this is not Delphi compatible, I think this should be
in Objpas mode only.

Michael.
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to