[fpc-pascal] interface inheritance implementation

Graeme Geldenhuys mailinglists at geldenhuys.co.uk
Mon Oct 12 15:46:25 CEST 2015


On 2015-10-11 20:29, David Emerson wrote:
> So I would like to use an interface that inherits from another 
> interface, and ... well rather than describe what I'm trying to do, I 
> think code speaks best.


Attached is a working example, tested under Delphi 7 and FPC 2.6.4. It
is slightly different to your example, but might give you an idea of how
to solve your issue.


Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
-------------- next part --------------
program Project2;

{$IFNDEF FPC} {$APPTYPE CONSOLE} {$ENDIF}

uses
  SysUtils;

type

    IParent = interface(IInterface)
    ['{85A340FA-D5E5-4F37-ABDD-A75A7B3B494C}']
      procedure DoSomething;
    end;

    IChild = interface(IParent)
    ['{15927C56-8CDA-4122-8ECB-920948027015}']
      procedure DoSomethingElse;
    end;

    TGrandParent = class(TInterfacedObject)
    end;

    TParent = class(TGrandParent)
    end;

    TChildDelegate = class(TInterfacedObject, IChild)
    private
      procedure DoSomething;             // IParent interface
      procedure DoSomethingElse;         // IChild interface
    end;

    TChild = class(TParent, IChild)
    private
      FChildDelegate: TChildDelegate;
    public
      constructor Create;
      property ChildDelegate: TChildDelegate read FChildDelegate implements IChild;
    end;



{ TChildDelegate }

procedure TChildDelegate.DoSomething;
begin
  writeln('>> TChildDelegate.DoSomething');
end;

procedure TChildDelegate.DoSomethingElse;
begin
  writeln('>> TChildDelegate.DoSomethingElse');
end;

procedure CallDoSomething(Parent: TParent);
var
  parentAsChild: IChild;
begin
  if Parent.GetInterface(IChild, parentAsChild) then
    parentAsChild.DoSomething;
end;

var
  c: TChild;
{ TChild }

constructor TChild.Create;
begin
  FChildDelegate := TChildDelegate.Create;
end;


begin
  c := TChild.Create;
  try
    CallDoSomething(c);
  finally
    c.Free;
  end;
  ReadLn;
end.
 


More information about the fpc-pascal mailing list