[fpc-pascal] interface inheritance implementation

David Emerson dle3ab at angelbase.com
Sun Oct 11 21:29:42 CEST 2015


Not sure what subject line to use to summarize what I'm trying to do 
here, but that is kind of related :)

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.

type

// here is the first interface
i_adder = interface
   ['{01234567-2222-407C-8133-987654FEDCBA}']
   function add (a, b : longint) : longint;
   end;

// this next interface inherits from i_adder, and thus does two things
i_multiplier_and_adder = interface (i_adder)
   ['{A1234567-2222-407C-8133-987654FEDCBA}']
   function multiply (a, b : longint) : longint;
   end;

t_base = TInterfacedObject;

// now an implementing class for i_adder
t_adder = class (t_base, i_adder)
   function add (a, b : longint) : longint;
   end;

// and finally the implementing class for i_multiplier_and_adder
t_multiplier_and_adder = class (tbase, i_multiplier_and_adder)
   f_adder : i_adder;
   function multiply (a, b : longint) : longint;
// THIS PROPERTY DOES NOT WORK:
   property adder : i_adder read f_adder implements i_adder;
   end;


The structure I have is like... I have one instance of t_adder, and I 
want to have several instances of t_multiplier_and_adder that all use 
the one instance of t_adder as their hook.

And in my real code, t_adder has 6 methods or so, thus it becomes 
cumbersome to implement all those in t_multiplier_and_adder, each just 
calling the function on the t_adder that I'd like to use as a hook.

Do I have to implement all of the individual i_adder methods for 
t_multiplier_and_adder? Is there no shortcut?


Thanks!
~David.





More information about the fpc-pascal mailing list