[fpc-pascal] Extend multiple classes with same code

Sven Barth pascaldragon at googlemail.com
Fri May 18 23:16:52 CEST 2012


On 18.05.2012 20:38, Zaher Dirkey wrote:
> Hi,
>
> I have 2 objects inherited from the base one
>
> T_A = class(TObject);
>
> T_B1 = class(T_A);
> T_B2 = class(T_A);
> T_B3 = class(T_A);
>
> Now if I want to extend T_B1 it is easy to inherit it to T_C1 = class
> (T_B1) and add many functions to it, but.
>
> I want the same extend T_B2  not T_B3,
>
> 1 - The first idea is adding this functions to T_A, but it is not my
> class to modify it, or it is protected from my modify.
> 2 - Copy and paste this functions to T_B2, but in that case I need every
> time to fix functions in B1 I must do it in B2, that make it so hard,
> and not good programming quality.
>
> Is there any new idea to do that, what about Helper, generics, i read
> about it i felt it is not fit to my problem (or i am wrong).
>
> Thanks in advance

Helper types might help you here.

E.g.

type
   T_AHelper = class helper for T_A
     procedure YourNewProc;
   end;

procedure T_AHelper.YourNewProc;
begin
   ...
end;

var
   t: T_B1;
begin
   t.YourNewProc;
end;

You can access public, published and (strict) protected members using a 
class helper.

The helper needs to be in scope ( = it's unit needs to be in the uses) 
if you want to use one of it's methods on a instance of T_A (or T_B).

Regards,
Sven



More information about the fpc-pascal mailing list