BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

TList?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Rory Walsh
Guest





PostPosted: Sat Mar 26, 2005 1:04 pm    Post subject: TList? Reply with quote



Hi, I am trying to work with the TList class and I have a little
problem, basically when I run the code below all of the items in my
TList are the same? I have commented the code to make it clear. Can
anyone spot the problem? I hope it's nothing really basic? Cheers.


__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
incr=0;
listofnames = new TList;
temp = new simplist;//A simple class containing an AnsiString
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
temp->name = "item "+(AnsiString)inc;
listofnames->Add(temp);
incr++;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
for(int i =0;i<listofnames->Count;i++)
{
temp = (node *)listofnames->Items[i];
Memo1->Lines->Add(temp->name);
}
}
Back to top
Rory Walsh
Guest





PostPosted: Sat Mar 26, 2005 1:08 pm    Post subject: Re: TList? Reply with quote



Sorry there was a mistake in that last post, here is the code as I have it,


TForm1 *Form1;
simplist* temp;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
listofnames = new TList;
temp = new simplist; //simple class with an ansistring called 'name'
inc=0;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
temp->name = "test"+(AnsiString)inc;
listofnames->Add(temp);
inc++;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
for(int i =0;i<listofnames->Count;i++)
{
temp = (simplist *)listofnames->Items[i];
Memo1->Lines->Add(temp->name);
}
}
Back to top
Alisdair Meredith
Guest





PostPosted: Sat Mar 26, 2005 1:22 pm    Post subject: Re: TList? Reply with quote



Rory Walsh wrote:

Quote:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
temp->name = "test"+(AnsiString)inc;
listofnames->Add(temp);
inc++;
}

This looks like your problem, right here <g>

temp is the same object, at the same address, every time you add it.
You change its contents many times, but when you call Add on TList it
takes the pointer variable temp, an address, and adds it into the list.

You need to create new objects each time, and don't forget to manage
their memory and free them when done.

An alternative would be to use an Standard Library container, such as
vector or list. These containers store objects by value, so you do not
need to manage memory as with a TList. However, if you have large
objects, passing by value can be quite inefficient, so you may want to
store pointers-to-object anyway. In that case, I would recommend
getting hold of the Boost libraries and using the shared_ptr template,
which again will manage the memory on your behalf.

AlisdairM(TeamB)

Back to top
Rory Walsh
Guest





PostPosted: Sat Mar 26, 2005 1:45 pm    Post subject: Re: TList? Reply with quote

Cheers. I don't know why, but for some reason I though it was only
taking the current values. What I am trying to do it implement a simple
linked list? I thought that instead of doing it all at a low level that
there might be some Builder components that would make it easier. Thanks
again,

Rory.


Alisdair Meredith wrote:
Quote:
Rory Walsh wrote:


void __fastcall TForm1::Button1Click(TObject *Sender)
{
temp->name = "test"+(AnsiString)inc;
listofnames->Add(temp);
inc++;
}


This looks like your problem, right here
temp is the same object, at the same address, every time you add it.
You change its contents many times, but when you call Add on TList it
takes the pointer variable temp, an address, and adds it into the list.

You need to create new objects each time, and don't forget to manage
their memory and free them when done.

An alternative would be to use an Standard Library container, such as
vector or list. These containers store objects by value, so you do not
need to manage memory as with a TList. However, if you have large
objects, passing by value can be quite inefficient, so you may want to
store pointers-to-object anyway. In that case, I would recommend
getting hold of the Boost libraries and using the shared_ptr template,
which again will manage the memory on your behalf.

AlisdairM(TeamB)

Back to top
Duane Hebert
Guest





PostPosted: Sat Mar 26, 2005 3:00 pm    Post subject: Re: TList? Reply with quote


"Rory Walsh" <rorywalsh (AT) ear (DOT) ie> wrote

Quote:
Cheers. I don't know why, but for some reason I though it was only
taking the current values. What I am trying to do it implement a simple
linked list? I thought that instead of doing it all at a low level that
there might be some Builder components that would make it easier. Thanks
again,

Well if you follow Allisdair's example a std::list of boost::shared_ptr would
probably work well.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.