How do I use class helpers to access strict private members of a class?

前端 未结 2 812
眼角桃花
眼角桃花 2020-12-05 09:58

This is a follow-up question to: How to hide a protected procedure of an object?
(I\'m a bit fuzzy on the whole class helper concept)

Suppose I have a

2条回答
  •  执念已碎
    2020-12-05 10:38

    Up to, and including Delphi 10.0 Seattle, you can use a class helper to access strict protected and strict private members, like this:

    unit Shy;
    
    interface
    
    type
      TShy = class(TObject)
      strict private
        procedure TopSecret;
      private
        procedure DirtyLaundry;
      protected
        procedure ResistantToChange;
      end;
    

    unit NotShy;
    
    interface
    
    uses
      Shy;
    
    type
      TNotShy = class helper for TShy
      public
        procedure LetMeIn;
      end;
    
    implementation
    
    procedure TNotShy.LetMeIn;
    begin
      Self.TopSecret;
      Self.DirtyLaundry;
      Self.ResistantToChange;
    end;
    
    end.
    

    uses
      ..., Shy, NotShy;
    
    procedure TestShy;
    var
      Shy: TShy;
    begin
      Shy := TShy.Create;
      Shy.LetMeIn;
      Shy.Free;
    end;
    

    However, starting with Delphi 10.1 Berlin, this no longer works! Class helpers can no longer access strict protected, strict private or private members. This "feature" was actually a compiler bug that Embarcadero has now fixed in Berlin. You are out of luck.

提交回复
热议问题