Board index » delphi » NEWBIE Nested declaration/circular reference? NEWBIE

NEWBIE Nested declaration/circular reference? NEWBIE

Maybe someone can throw some light on this code, it is an example from
Marco Cantu's super book,  'Mastering Delphi' (Page 164)

unit Unit1;

interface
TYPE
  THusband = Class;
  TWife    = Class
    Husband : THusband;
  END;

  THusband = Class
    Wife  : TWife;
  END;

implementation

END.

It raises a couple of problems for me:-

1.      Why is there a ';' after the first class declaration and not the
second? ( I have seen it the other way round.)

2.      What is the purpose of declaring a variable of the same type as
the class that owns that variable, can you give an example of where this
would be advantageous ? ( Forget this one, I think I see it. it is two
seperate declarations!)

3.      It appears that THusband is declared twice with different
structure, how is this possible?

************************************************************************
AHHH  is the example simply showing that you can have a circular
reference WITHOUT recourse to a forward declaration.  

I am still puzzled as to how it does not flag a duplicate identifier
error or a NO forward declaration error.... This Delphi is a bit
powerful!
************************************************************************

Comments welcome, I am gathering speed with this SUPERB language but
there are a few ideas that are slow to lodge in an old brain!

Thank you for your help and indulgence!

Cheers,  Jim

Jim Barr        Machine Conversation
                http://www.wandana.demon.co.uk/
                Best is the enemy of good enough

                Leaves rustle,   blades turn,    water moves

 

Re:NEWBIE Nested declaration/circular reference? NEWBIE


Quote
Jim Barr wrote:
> Maybe someone can throw some light on this code, it is an example from
> Marco Cantu's super book,  'Mastering Delphi' (Page 164)
> unit Unit1;
> interface
> TYPE
>   THusband = Class;
>   TWife    = Class
>     Husband : THusband;
>   END;

>   THusband = Class
>     Wife  : TWife;
>   END;

> implementation

> END.

[...]

Quote
> 1.      Why is there a ';' after the first class declaration and not the
> second? ( I have seen it the other way round.)
> 2.      What is the purpose of declaring a variable of the same type as
> the class that owns that variable, can you give an example of where this
> would be advantageous ? ( Forget this one, I think I see it. it is two
> seperate declarations!)
> 3.      It appears that THusband is declared twice with different
> structure, how is this possible?
> I am still puzzled as to how it does not flag a duplicate identifier
> error or a NO forward declaration error.... This Delphi is a bit
> powerful!

[...]

The thing is that the first declaration of THusband is indeed a forward
declaration.  That's why there's a ";" after the first declaration.  There's
no ";" after the second because the declaration is not done yet (the
fields of the object have not yet been defined).
Thus the second declaration of THusband  is the one that the program will
use, the first one simply lets you use the type in TWife.

Personally I find the syntax here confusing as well but since Borland
got rid of the pointer declarations for object (which took some getting
used to but I quite like) something like this was indeed needed.

Have fun!

Jochen

Re:NEWBIE Nested declaration/circular reference? NEWBIE


In article <NVgSYFAfWh4yE...@wandana.demon.co.uk>, Jim Barr
<JimB...@wandana.demon.co.uk> writes

Please do not worry about replying to this one, I have examined it more
closely and had an email about it...

I can see how it works.. Very Clever.

Cheers,  Jim

Jim Barr        Machine Conversation
                http://www.wandana.demon.co.uk/
                Best is the enemy of good enough

                Leaves rustle,   blades turn,    water moves

Other Threads