Jump to content
Zoë Peterson

Compiler accepts virtual methods in class helpers?

Recommended Posts

This is "Does anyone know WTF is supposed to happen?", not "I want to do this and think it should work":


The code below has two class helpers, one descending from the other.  The parent one has a virtual method and the descendent overrides it.  The compiler accepts it without any errors/warnings/hints.  Actually running prints

 

Quote

Bar

BaseClass.Foo

---

SubClass.Foo

 

So the parent helper doesn't care about it being virtual, and the subclass ignores the 'inherited' call.

 

If you don't actually create the object before calling Obj.Foo, it raises an access violation in  System.pas::_GetHelperIntf().  

 

I found an old blog post from Joe White where he stumbled on it back in 2007, but back then _GetHelperIntf() was essentially a no-op, and it now has code.  Obviously since class helpers don't have their own VMTs, the code doesn't make sense, but why does the compiler accept it?  I don't see anything in the class and record helper help about declaring interfaces, and AFAIK they still don't support interface helpers.  Anyone have any idea what's going on here?

program ClassHelperTest;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

type
  TObjHelper = class helper for TObject
	procedure Foo; virtual;
	procedure Bar;
  end;

  TSubObjHelper = class helper(TObjHelper) for TObject
	procedure Foo; override;
  end;

procedure TObjHelper.Bar;
begin
  WriteLn('Bar');
  Foo;
end;

procedure TObjHelper.Foo;
begin
  WriteLn('BaseClass.Foo');
end;

procedure TSubObjHelper.Foo;
begin
  inherited;
  WriteLn('SubClass.Foo');
end;

var
  Obj: TObject;
begin
  Obj := TObject.Create;
  Obj.Bar;
  WriteLn('---');
  Obj.Foo;
  Obj.Free;
end.

 

Edited by Zoë Peterson
  • Like 1

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

×