[DUG] Accessing an ancestor class private field

Kyley Harris kyleyharris at gmail.com
Fri Jun 10 11:17:15 NZST 2005


What you are looking for is a hack! 
The hack is like this.

in the unit that needs to access the private field

THackCollection = class(TPersistent)
  private
    FItemClass: TCollectionItemClass;
    FItems: TList;
    FUpdateCount: Integer;
    FNextID: Integer;
    FPropName: string;
end;

This THackCollection is declared exactly as the top entries of
TCollection, and derives from the same class;

var
  Obj:TCollection;

begin
  THackCollection(Obj).FItems.Free;
  THackCollection(Obj).FItems := TMySpecialList.Create;


this is only safe with a guarantee that the object layouts (so memory
layouts are identical)

On 6/10/05, Todd Martin <toddm at kol.co.nz> wrote:
>  
> Thanks Conor. I understand that, but I thought there was a way to get at
> private fields as well. I know it defeats the purpose of making a field
> private, but that's another discussion. 
>   
>  
> ----- Original Message ----- 
> From: Conor Boyd 
> To: 'NZ Borland Developers Group - Delphi List' 
> Sent: Friday, June 10, 2005 10:44 AM 
> Subject: RE: [DUG] Accessing an ancestor class private field 
> 
>  
> If I understand you correctly... 
>   
> You can increase the visibility of a property in sub-classes by simply
> redeclaring the property in a declaration section with greater visibility
> (e.g. redeclaring a protected property in the public section), but I don't
> think you can for fields. 
>   
> A title in the Delphi 5 help called "Property overrides and redeclarations"
> may provide more information. 
>   
> Cheers, 
>   
> Conor
>  
>  ________________________________
>  From: delphi-bounces at ns3.123.co.nz [mailto:delphi-bounces at ns3.123.co.nz] On
> Behalf Of Todd Martin
> Sent: Friday, 10 June 2005 10:28 a.m.
> To: NZ Borland Developers Group - Delphi List
> Subject: [DUG] Accessing an ancestor class private field
> 
>  
>  
>  
> I seem to recall someone posting a message showing a hack of how to access a
> private field in an ancestor class. 
> Something along the lines of redeclaring the field in the descendant class.
> I've tried to find the solution, but with no luck. 
> Can someone please remind me. Thanks. 
> 
>  ________________________________
>  
> 
> _______________________________________________
> Delphi mailing list
> Delphi at ns3.123.co.nz
> http://ns3.123.co.nz/mailman/listinfo/delphi
> 
> 
>  
> Internal Virus Database is out-of-date.
> Checked by AVG Anti-Virus.
> Version: 7.0.322 / Virus Database: 267.5.1 - Release Date: 2/06/2005
> 
> 
> _______________________________________________
> Delphi mailing list
> Delphi at ns3.123.co.nz
> http://ns3.123.co.nz/mailman/listinfo/delphi
> 
> 
> 


-- 
Kyley Harris
Harris Software
+64-9-8455274
+64-21-671-821



More information about the Delphi mailing list