 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Maurice Anderson Guest
|
Posted: Tue May 09, 2006 4:14 am Post subject: TreeView->Data question |
|
|
If I do something like this:
TTreeNode *CountNode = TreeView1->Selected->getFirstChild();
ListView1->Items->Clear();
while(CountNode){
TListItem *ListItem = ListView1->Items->Add();
ListItem->Caption = CountNode->Text;
ListItem->ImageIndex = CountNode->ImageIndex;
ListItem->Data = CountNode->Data;
CountNode = CountNode->getNextSibling();
}
Are there now 2 copies of Data with one in CountNode->Data and the other in
ListItem->Data? Or is it shared between the two somehow? If I make some
kind of change to CountNode->Data is the ListItem->Data changed as well? On
that note, how would I create an independant/non-shared copy in
ListItem->Data?
And what happens if I do this:
void __fastcall TMDIChild::TreeView1Deletion(TObject *Sender,
TTreeNode *Node)
{
delete Node->Data;
}
Is ListItem->Data deleted as well?
Thanks |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue May 09, 2006 7:14 am Post subject: Re: TreeView->Data question |
|
|
"Maurice Anderson" <mauriceanderson (AT) hotmail (DOT) com> wrote in message
news:44600a76$1 (AT) newsgroups (DOT) borland.com...
| Quote: | If I do something like this:
snip
Are there now 2 copies of Data with one in CountNode->Data
and the other in ListItem->Data?
|
There are two pointers containing the same memory address that is being
pointed to. There is not two copies of the actual memory being pointed to.
| Quote: | If I make some kind of change to CountNode->Data is the ListItem->Data
changed as well?
|
The memory address that is stored in the pointer is not effected at all.
The memory that is being pointed to, on the other hand, effected both as
long as they both continue to point to the same memory block.
| Quote: | On that note, how would I create an independant/non-shared copy
in ListItem->Data?
|
What exactly are you storing in the TreeNode's Data property to begin with?
| Quote: | And what happens if I do this:
snip |
That code has undefined behavior. The Data property is a void* pointer,
which has no type information. The 'delete' operator needs to know the
specific type of data that is being freed in order to free it correctly.
You must type-cast the Data pointer to the actual type that you stored in
it.
| Quote: | Is ListItem->Data deleted as well?
|
Assuming you type-cast the void* pointer properly, then the List Item would
be left pointing to an invalid memory block. If you later call 'delete' on
the TListItem's Data property, then that would be very bad as you would be
freeing the same memory block twice.
Gambit |
|
| 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
|
|