Mutually dependent classes...

Hi All,

I want to create two classes A and B which are mutually dependent. Let us say, the requirement is that A's constructor needs an object of type B as argument and B's constructor needs an object A as argument.
In C++, I can use forward declaration and still have declartions of both A and B in different *.c files.
But in Delphi is it necessary that if I use forward declaration, both A and B should be in the same Unit? Is there any workaround to have two mutually dependent class in two Units? Or is it impossible?

regards and thanks
Kanib

Comments

  • : Hi All,
    :
    : I want to create two classes A and B which are mutually dependent. Let us say, the requirement is that A's constructor needs an object of type B as argument and B's constructor needs an object A as argument.
    : In C++, I can use forward declaration and still have declartions of both A and B in different *.c files.
    : But in Delphi is it necessary that if I use forward declaration, both A and B should be in the same Unit? Is there any workaround to have two mutually dependent class in two Units? Or is it impossible?
    :
    : regards and thanks
    : Kanib
    :
    It is not directly possible to have A and B in two different units. You can however use the TObject as parameter in one of the constructors, and check if the object is of the correct type. Here is a sample code consisting of 2 units:
    [code]
    unit Unit1;

    interface

    type
    TA = class(TObject)
    private
    protected
    public
    constructor Create(B: TObject);
    end;

    implementation

    uses
    SysUtils, Unit2;

    { TA }

    constructor TA.Create(B: TObject);
    var
    TempB: TB;
    begin
    inherited Create;
    if B is TB then
    begin
    TempB := TB(B);
    // Continue construction
    end else
    raise Exception.Create('B not of TB');
    end;

    end.
    [/code]
    [code]
    unit Unit2;

    interface

    type
    TB = class(TObject)
    private
    protected
    public
    constructor Create(A: TObject);
    end;

    implementation

    uses
    SysUtils, Unit1;

    { TB }

    constructor TB.Create(A: TObject);
    var
    TempA: TA;
    begin
    inherited Create;
    if A is TA then
    begin
    TempA := TA(A);
    // Continue construction
    end else
    raise Exception.Create('A not of TA');
    end;

    end.
    [/code]

  • : : Hi All,
    : :
    : : I want to create two classes A and B which are mutually dependent. Let us say, the requirement is that A's constructor needs an object of type B as argument and B's constructor needs an object A as argument.
    : : In C++, I can use forward declaration and still have declartions of both A and B in different *.c files.
    : : But in Delphi is it necessary that if I use forward declaration, both A and B should be in the same Unit? Is there any workaround to have two mutually dependent class in two Units? Or is it impossible?
    : :
    : : regards and thanks
    : : Kanib
    : :
    : It is not directly possible to have A and B in two different units. You can however use the TObject as parameter in one of the constructors, and check if the object is of the correct type. Here is a sample code consisting of 2 units:
    : [code]
    : unit Unit1;
    :
    : interface
    :
    : type
    : TA = class(TObject)
    : private
    : protected
    : public
    : constructor Create(B: TObject);
    : end;
    :
    : implementation
    :
    : uses
    : SysUtils, Unit2;
    :
    : { TA }
    :
    : constructor TA.Create(B: TObject);
    : var
    : TempB: TB;
    : begin
    : inherited Create;
    : if B is TB then
    : begin
    : TempB := TB(B);
    : // Continue construction
    : end else
    : raise Exception.Create('B not of TB');
    : end;
    :
    : end.
    : [/code]
    : [code]
    : unit Unit2;
    :
    : interface
    :
    : type
    : TB = class(TObject)
    : private
    : protected
    : public
    : constructor Create(A: TObject);
    : end;
    :
    : implementation
    :
    : uses
    : SysUtils, Unit1;
    :
    : { TB }
    :
    : constructor TB.Create(A: TObject);
    : var
    : TempA: TA;
    : begin
    : inherited Create;
    : if A is TA then
    : begin
    : TempA := TA(A);
    : // Continue construction
    : end else
    : raise Exception.Create('A not of TA');
    : end;
    :
    : end.
    : [/code]
    :
    :

  • : : : Hi All,
    : : :
    : : : I want to create two classes A and B which are mutually dependent. Let us say, the requirement is that A's constructor needs an object of type B as argument and B's constructor needs an object A as argument.
    : : : In C++, I can use forward declaration and still have declartions of both A and B in different *.c files.
    : : : But in Delphi is it necessary that if I use forward declaration, both A and B should be in the same Unit? Is there any workaround to have two mutually dependent class in two Units? Or is it impossible?
    : : :
    : : : regards and thanks
    : : : Kanib
    : : :
    : : It is not directly possible to have A and B in two different units. You can however use the TObject as parameter in one of the constructors, and check if the object is of the correct type. Here is a sample code consisting of 2 units:
    : : [code]
    : : unit Unit1;
    : :
    : : interface
    : :
    : : type
    : : TA = class(TObject)
    : : private
    : : protected
    : : public
    : : constructor Create(B: TObject);
    : : end;
    : :
    : : implementation
    : :
    : : uses
    : : SysUtils, Unit2;
    : :
    : : { TA }
    : :
    : : constructor TA.Create(B: TObject);
    : : var
    : : TempB: TB;
    : : begin
    : : inherited Create;
    : : if B is TB then
    : : begin
    : : TempB := TB(B);
    : : // Continue construction
    : : end else
    : : raise Exception.Create('B not of TB');
    : : end;
    : :
    : : end.
    : : [/code]
    : : [code]
    : : unit Unit2;
    : :
    : : interface
    : :
    : : type
    : : TB = class(TObject)
    : : private
    : : protected
    : : public
    : : constructor Create(A: TObject);
    : : end;
    : :
    : : implementation
    : :
    : : uses
    : : SysUtils, Unit1;
    : :
    : : { TB }
    : :
    : : constructor TB.Create(A: TObject);
    : : var
    : : TempA: TA;
    : : begin
    : : inherited Create;
    : : if A is TA then
    : : begin
    : : TempA := TA(A);
    : : // Continue construction
    : : end else
    : : raise Exception.Create('A not of TA');
    : : end;
    : :
    : : end.
    : : [/code]
    : :
    : :
    :
    :
    Thank You!!!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories