Board index » delphi » Parent/Child classes

Parent/Child classes

I have the following scenario

TClassA = Class(TObject)
private
    pChildForms: TList;
end;

TClassB = Class(TObject)
private:
    pParent : TClassA;
end;

If Both TClassA and TClassB are defined in separate units, I get a circular
reference.  What is the most elegant solution ?  The ones I have found are:

use a class placeholder for the interface section so.....

TClassAPtr = Class(TObject)

and ClassB's definition is

TClassB = Class(TObject)
private:
    pParent : TClassAPtr;
end;

This works and I cast the code in the Implementation section to get it to
work.  I can do a similar thing by having a pointer to a TObject instead of
creating my own Place Holder.

Help!

MarkM

 

Re:Parent/Child classes


The Parent only needs to be a TObject.  This is the simplest solution.

TClassB = Class(TObject)
private
   pParent : TObject;
end;

This way you can assign any type of object to be the parent.

Another way to do this would be to place both objects in the same unit.

Mark Mamone <mark.mam...@syntegra.bt.co.uk> wrote in article
<7aeidk$fs...@pheidippides.axion.bt.co.uk>...

Quote
> I have the following scenario

> TClassA = Class(TObject)
> private
>     pChildForms: TList;
> end;

> TClassB = Class(TObject)
> private:
>     pParent : TClassA;
> end;

> If Both TClassA and TClassB are defined in separate units, I get a
circular
> reference.  What is the most elegant solution ?  The ones I have found
are:

> use a class placeholder for the interface section so.....

> TClassAPtr = Class(TObject)

> and ClassB's definition is

> TClassB = Class(TObject)
> private:
>     pParent : TClassAPtr;
> end;

> This works and I cast the code in the Implementation section to get it to
> work.  I can do a similar thing by having a pointer to a TObject instead
of
> creating my own Place Holder.

> Help!

> MarkM

Re:Parent/Child classes


Im Artikel <7aeidk$fs...@pheidippides.axion.bt.co.uk>, "Mark Mamone"
<mark.mam...@syntegra.bt.co.uk> schreibt:

Quote
>If Both TClassA and TClassB are defined in separate units, I get a circular
>reference.  What is the most elegant solution ?

If both classes must have detailed knowledge about each other, the declarations
*must* reside in the same unit.

If you want to separate the implementation from the declaration, you can
declare abstract classes in a common unit, and derive and implement the
actually used classes in separate modules.

You can also declare interfaces for both classes, and implement these similarly
to the abstract classes mentioned above.

DoDi

Other Threads