[fpc-pascal] Problem accessing Class Method with abstraction

Andrew Brunner andrew.t.brunner at gmail.com
Sun Oct 17 15:15:07 CEST 2010


On Sun, Oct 17, 2010 at 3:33 AM, Michael Van Canneyt
<michael at freepascal.org> wrote:

<SNIP>
// Revised code

CCoreObject=Class(TCoreObject);
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;

procedure TCoreObjects.Load;
var
  iLcv:integer;
  coGeneric:TPersistentClass;
  ItemP:PCoreObjectItem;
  coItem:TCoreObject;
  id:IStringsAdapter;
begin
  if FInfo.Loaded then exit;
  for iLcv:=0 to High(CoreObjectItems) do begin
    ItemP:=CoreObjectItems[iLcv];
    coGeneric:=GetClass(ItemP^.ClassName);
    if (coGeneric<>nil) then
      Add(iLcv,coGeneric);
  end;
  FInfo.Loaded:=True;
end;

procedure TCoreObjects.Add(iCoreItemIndex:integer; Item:TPersistentClass);
var
  iIndex:integer;
  coItem:TCoreObject;
begin
  if Item.InheritsFrom(TCoreObject) then begin
    coItem:=Item.Create as TCoreObject;
    iIndex:=Add(coItem);
    Copy(CoreObjectItems[iCoreItemIndex]^,coItem.Header);
    coItem.OwnerP:=@FInfo;
    coItem.Initialize;
  end;
end;

<OMITTED>

procedure VerifyIDs(Var Module:TDBMSModule);
var
  iLcv:integer;
  ItemP:PCoreObjectItem;
  coGeneric:TPersistentClass;
  coItem:CCoreObject;
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 CCoreObject; // did not compile
      coItem:=TCoreObjectClass(coGeneric); // Compiles
      coItem.VerifyIDs(Module); // SIGEV
    end;
  end;
end;

It's crashing with SIGEV error.  I think b/c at that point the class
reference becomes a TCoreObjectClass with pointer to abstracted method
not the TAdminCore.VerifyIDs method.

Take TCoreObjects.Add(iCoreItemIndex:integer; Item:TPersistentClass);
for a point of discussion...  It has no problems loading and creating
instances of descendant core objects as they are presently running and
referenced throughout the project.  So while this method accepts the
as TCoreObject the as keyword does not work for class references.  How
can I leave these class references as generic but yet still access the
implmented method of the descendant class?



More information about the fpc-pascal mailing list