[fpc-pascal] Extend multiple classes with same code

Sven Barth pascaldragon at googlemail.com
Sat May 19 16:52:55 CEST 2012


On 19.05.2012 16:28, Zaher Dirkey wrote:
> On Sat, May 19, 2012 at 2:10 AM, Jorge Aldo G. de F. Junior
> <jagfj80 at gmail.com <mailto:jagfj80 at gmail.com>> wrote:
>
>     You simply cant do it. Not even multiple inheritance would help you in
>     this case.
>
>     You will have to extend each class by hand.
>
>     Multiple inheritance would allow you to join back branches in the
>     class hierarchy, but in this case you want to add the same
>     functionality to multiple branches, but keep the branches separate
>     entities. This is impossible with current freepascal OOP and i dont
>     think this is even a good thing to have.
>
>
> I have no Idea about "multiple inheritance".
>
> I am thinking now if i can assign a helper to more than one class by
> make base helper, then inherit it for every class B1 and B2.
> I will try and post a feedback.

Yes, you can do that basically:

type
   T_AHelper = class helper for T_A

   end;

   T_B1Helper = class helper(T_AHelper) for T_B1

   end;

   T_B2Helper = class helper(T_AHelper) for T_B2

   end;

Please not that the extended classes (in this case T_B1 and T_B2) need 
to inherited from T_A, otherwise the compiler will complain.

Regards,
Sven




More information about the fpc-pascal mailing list