Board index » delphi » Link List of Objects in Delphi 3

Link List of Objects in Delphi 3

Hi

I am trying to create a link
list of objects in Delphi 3,
but require a pointer to the
object. This at first seemed
simple just do

var
   Item  : TListItem;
   ItemPtr : pTItemPtr;
begin
   Item:=TListItem.Create;
   ItemPtr:=@Item;
   ....
   AddToLinkList(MyObjectPtr);

with the object/pointers
defined as

type
     pTItemPtr : ^TListItem;
     TListItem=class
     public
       Number : Integer;
       NextItemPtr :
pTListItem;
       PrevItemPtr :
pTListItem;
       IsLastItem : Boolean;
       IsFirstItem : Boolean;
       constructor  create;
override;
       destructor   destroy;
override;
     end;
....

AddToLinkList looked as follow
(FirstItemPtr and LastItemPtr
is fields of my linklist
object)
....

procedure
TLinkListlist.AddToLinkList(AddItemPtr:
pTListItem);
begin

FirstItemPtr^.PrevItemPtr:=AddItemPtr;

LastItemPtr^.NextItemPtr:=AddItemPtr;

LastItemPtr^.IsLastItem:=false;

AddItemPtr^.PrevItemPtr:=LastItemPtr;

AddItemPtr^.NextItemPtr:=FirstItemPtr;

AddItemPtr^.IsFirstItem:=false;

AddItemPtr^.IsLastItem:=true;

LastItemPtr:=AddItemPtr;
end;

The abovementioned code does
not work because Delphi
creates a Reference to the
object in Item and the pointer
ItemPtr then contains the
adress of the reference and
not that of the object.

A second solution then seemed
to work with the reference
contained in item this changed
the code to

var
   Item  : TListItem;
begin
   Item:=TListItem.Create;
   ....
   AddToLinkList(Item);

with the objects/pointers
defined as

type
     TListItem=class
     public
       Number : Integer;
       NextItemRef :
TListItem;
       PrevItemRef :
TListItem;
       IsLastItem : Boolean;
       IsFirstItem : Boolean;
       constructor  create;
override;
       destructor  
destroyItem;
     end;
....

AddToLinkList looked as follow
(FirstItemRef and LastItemRef
is fields of my link list
object)
....

procedure
TLinkListlist.AddToLinkList(AddItemRef:
TListItem);
begin

FirstItemRef.PrevItemRef:=AddItemRef;

LastItemRef.NextItemRef:=AddItemRef;

LastItemRef.IsLastItem:=false;

AddItemRef.PrevItemRef:=LastItemRef;

AddItemRef.NextItemRef:=FirstItemRef;

AddItemRef.IsFirstItem:=false;

AddItemRef.IsLastItem:=true;

LastItemRef:=AddItemRef;
end;

This would have worked if I
had some control over Delphis
dereferencing of the
reference. As this occurs
automatically the assignment
of the AddItemRef to the
LastItemRef.NextItemRef
changes the object referenced
by LastItemRef.NextItemRef
instead of assigning the
reference adress to the
NextItemReference (a similar
thing happens in just about
all of the assignments) - thus
not very usefull for my
problem

This depleted my source of
knowledge. If there is any one
out there that solved the
problem of linked list (for
Objects) in Delphi please let
me know.

Thanx
Dirk
dvdme...@firga.sun.ac.za

 

Re:Link List of Objects in Delphi 3


I could be wrong here... What I think you are trying to do is explicitly
dereference an object's pointer. However, when you create an object, using
the (TObject).Create method, this is already a pointer (implicit
dereferencing..). Thus, you are pointing to a pointer that points to the
object..... When creating an object, an explicit dereference is not needed.
What I do not understand, though, is that you SHOULD be able to explicitly
dereference if you want to...

Re:Link List of Objects in Delphi 3


In article <70mi9i$ch...@ash.prod.itd.earthlink.net>, "Philip Wieland"

Quote
<pwiel...@earthlink.com> writes:
> However, when you create an object, using
>the (TObject).Create method, this is already a pointer

Yes. So why not List.Add(SomeObject.Create)?

Charles Johnson

Re:Link List of Objects in Delphi 3


Quote
CEHJohnson wrote:

> Yes. So why not List.Add(SomeObject.Create)?

> Charles Johnson

That should be fine, and is a nice way of doing it.

For languages (such as modula-3) which have objects that don't have
constructors (ie constructors that return the value of self), just a
default virtual "Init" procedure, the standard way of instantating
objects is:

MyFoo:=NEW(TFoo).Init;

MH.

--
Martin Harvey

mailto:mar...@aziraphale.demon.co.uk
http://www.harvey27.demon.co.uk/mch24/

Undoubtedly, someone can learn Object Oriented Programming from
reading code - after all Alan Kay learned OO by deciphering
an 80 page Simula program thinking it was Algol.
However, most people are not in the same league as Alan.
- Bjarne Stroustrup

Other Threads