[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.


// here is the first interface
i_adder = interface
   function add (a, b : longint) : longint;

// this next interface inherits from i_adder, and thus does two things
i_multiplier_and_adder = interface (i_adder)
   function multiply (a, b : longint) : longint;

t_base = TInterfacedObject;

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

// 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;
   property adder : i_adder read f_adder implements i_adder;

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?


More information about the fpc-pascal mailing list