Jump to content
dummzeuch

Hack to access an ancestor's private method

Recommended Posts

Given the following class:

unit Ancestors;

interface

type
  TAncestorClass = class
  private
    procedure SomeMethod;
  end;

[... stuff ...]

... and a descendant class that is declared in a different unit:

unit Descendants;

interface

uses
  Ancestors;

type
  TDescendant = class(TAncestor)
  protected
    procedure SomeOtherMethod;
  end;

// [...]

implementation

procedure TDescendant.SomeOtherMethod;
begin
  SomeMethod; // <-- this won't compile
end;

I need to call the private TAncestor.SomeMethod  from TDescendant.SomeOtherMethod. This of course won't compile because TAncestor.SomeMethod is declared as private and I cannot change that declaration (It's part of the Delphi VCL).

 

My solution so far is to copy the implementation to my unit, but I am pretty sure this would violate Embarcadero's copyright on that (non trivial) code if I were to release that source code. So I am looking for a different solution.

 

It guess it would be possible to somehow patch my class to call the original code, but I have never done something like that. Is there any documentation / blog post / example code on how to do that? I am aware that I would then possibly have to adapt this patch to every Delphi version.

 

Any hints?

 

Edit: Note: Class Helpers and extended RTTI are not an option here. This must work with Delphi 2007.

Edited by dummzeuch

Share this post


Link to post

Use the magic with statement:

 

With Self do SomeMethod

This works in all version of Delphi including recent ones.

Edited by pyscripter

Share this post


Link to post
23 minutes ago, pyscripter said:

Use the magic with statement:

 


With Self do SomeMethod

This works in all version of Delphi including recent ones.

Unfortunately that only works in class helpers.

Share this post


Link to post
Guest

I remember being a bit chocked when Emba changed that. You can still do it with "protected".

However, i seldom port big (deployed) projects between delphi versions.

And after a couple of years, the immediate thought [this should be so] obviously shows.

For newer projects, i have been forced to suggest a move of fields or methods to vendors.

That is normally not a big issue (pls move that up). And in the long run the result(s) are much cleaner.

IMHO: A project that NEEDS to override private members should not be migrated to newer versions.

As long as we are aware where this hack has been used, it should be an important consideration when deciding to port.

 

I know this won't help you, but anyways...

Share this post


Link to post
9 hours ago, dummzeuch said:

Unfortunately that only works in class helpers.

You can use a class helper to expose a private method of a parent class.

Share this post


Link to post
3 hours ago, pyscripter said:

You can use a class helper to expose a private method of a parent class.

No, I can't.

14 hours ago, dummzeuch said:

Note: Class Helpers and extended RTTI are not an option here. This must work with Delphi 2007.

Edited 14 hours ago by dummzeuch

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×