[fpc-pascal] Problem accessing Class Method with abstraction

Michael Van Canneyt michael at freepascal.org
Sun Oct 17 10:33:10 CEST 2010



On Sat, 16 Oct 2010, Andrew Brunner wrote:

> I've got a class factory problem I'm trying to resolve and it appears
> I'm at a point where it's probably better to ask for help.
>
>  TCoreObjects=class;
>  TCoreObject=Class (TPersistent)
>  protected
>    class procedure VerifyIDs(var Module:TDBMSModule); Virtual; abstract;
>  end;
>  TAdminCore=Class(TCoreObject)
>  protected
>    class procedure VerifiyIDs(var Module:TDBMSModule); override;
>  end;
>
> <SNIP>
>
> procedure VerifyIDs(Var Module:TDBMSModule);
> var
>  iLcv:integer;
>  ItemP:PCoreObjectItem; // Just record storing classname to load and
> (each core object implment is loaded just not detailed here for
> simplicity)
>  coGeneric:TPersistentClass;
>  coItem:TCoreObject;
> begin
>  for iLcv:=0 to High(CoreObjectItems) do begin
>    ItemP:=CoreObjectItems[iLcv];
>    coGeneric:=GetClass(ItemP^.ClassName);
>    if (coGeneric<>nil) and coGeneric.InheritsFrom(TCoreObject) then begin
>      coItem:=coGeneric as TCoreObject << COMPILER ERROR :-(

Of course it errors, you're trying to store a VMT in an object instance.

You chould declare

   TCoreObjectClass = Class (TCoreObject);

and
   coItem : TCoreObjectClass;

Then

   coItem:=TCoreObjectClass(coGeneric);
   coItem.VerifyIDs(Module);

Should work. Untested (not enough code provided) but I think the above should work.

Michael.



More information about the fpc-pascal mailing list