[fpc-pascal] Interfaces via implements keyword

Denis Golovan d.golovan.ml at gmail.com
Sun Nov 22 13:24:54 CET 2009


Hi all

I've got a non-obvious class hierarchy with interfaces involved.
The problem is that, I thought "property ... implements ...;" construction
fully substitute for implementing interface methods in class itself, but now
it's just not true.
I get different results in case implements is used and when it isn't.

If I comment implements implementation, my results are:
TClassBase.Print 3
TClassBase.Print 3
TClassBase.Print 3

If I use implements, my results are:
TClassBase.Print 3
TClassBase.Print 3
TClassBase.Print 30

See my example below.

I wonder if it is by design?
I have similar bug posted on bug tracker under
http://bugs.freepascal.org/view.php?id=14842

======================================
program project1;

{$mode objfpc}{$H+}

type
  IIntf1 = interface
    ['{925B72C3-B47D-432A-AED8-544B3306055B}']
  end;

  IIntf2 = interface
    ['{E5C9BAC0-CE6D-42A8-BC85-F0AA51360E83}']
    procedure Print;
  end;

  { TClassBase }

  TClassBase = class(TInterfacedObject, IIntf2)
    Int:Integer;

    procedure Print;
  end;

  { TClass1 }

  TClass1 = class(TClassBase, IIntf1)
    public
      constructor Create;
  end;

  { TClass2 }

  TClass2 = class(TClassBase, IIntf1)
  private
    FIntf: IIntf1;
  public
    constructor Create;

    property Intf:IIntf1 read FIntf implements IIntf1; //COMMENT THIS LINE
  end;

{ TClassBase }

procedure TClassBase.Print;
begin
  WriteLn('TClassBase.Print ', Int);
end;

constructor TClass2.Create;
var O:TClass1;
begin
  Int:=3;

  O:=TClass1.Create;
  O.Int:=30;
  FIntf:=O;
end;

{ TClass1 }

constructor TClass1.Create;
begin
  Int:=1;
end;

var C2:TClass2;
    I1:IIntf1;
    I2:IIntf2;
begin
  C2:=TClass2.Create;

  C2.Print;

  I1:=C2 as IIntf1;

  I2:=C2 as IIntf2;
  I2.Print;

  I2:=I1 as IIntf2;
  I2.Print;
end.
======================================

-- 
Best regards,
Denis Golovan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20091122/6c5f624e/attachment.html>


More information about the fpc-pascal mailing list