[DUG] Accessing an ancestor class private field

David Brennan dugdavid at dbsolutions.co.nz
Fri Jun 10 11:38:06 NZST 2005


There is a clever way of accessing a protected variable or method. 

 

To access protected variable FFoo of TSillyObject you do:

 

Type

  TAccessSillyObject = Class(TSillyObject);

 

.

 

Var

  LMySillyObject : TSillyObject;

Begin

  .

  LFooValue := TAccessSillyObject(LMySillyObject).FFoo;

  .

 

This works because protected attributes are accessible anywhere within the
unit which defines the class and since TAccessSillyObject is defined in your
unit once you typecast to it you can see the protected attribute. Bizarre
but it works.

 

Private variables are more difficult. I think Kyley's suggestion is the only
real way but it's dangerous if anything changes. Note that it doesn't
involve actually defining your objects as THackWhatever, it's all about
typecasting them as THackWhatever (even though they aren't). In that respect
it is similar to what I have described but it is going one step further in
that the typecast class defines a different structure.

 

David.

 

  _____  

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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://ns3.123.co.nz/pipermail/delphi/attachments/20050610/b90fdbe3/attachment.html


More information about the Delphi mailing list