 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rory Walsh Guest
|
Posted: Sat Mar 26, 2005 1:04 pm Post subject: TList? |
|
|
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
|
Posted: Sat Mar 26, 2005 1:08 pm Post subject: Re: TList? |
|
|
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
|
Posted: Sat Mar 26, 2005 1:22 pm Post subject: Re: TList? |
|
|
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
|
Posted: Sat Mar 26, 2005 1:45 pm Post subject: Re: TList? |
|
|
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
|
Posted: Sat Mar 26, 2005 3:00 pm Post subject: Re: TList? |
|
|
"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 |
|
 |
|
|
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
|
|