-
June 3rd, 2009, 03:11 AM
#1
regarding CTypedPtrArray,moving of array
Well am designing an application in which i am using CTypedPtrArray for saving an pointers of array now at any instant how can i delete any of the value at given index and then move the whole rest of the array,is their any other suggestion rather than moving of complete array........
Please help me...
-
June 3rd, 2009, 03:57 AM
#2
Re: regarding CTypedPtrArray,moving of array
Is there any reason to use CTypedPtrArray rather than CArray<...> of objects?
Victor Nijegorodov
-
June 3rd, 2009, 06:49 AM
#3
Re: regarding CTypedPtrArray,moving of array
yeah actually am not well acquainted with CArray<>...i have used this to save records of class that's derived from CObject....wel actually i solved my other problems the only problem am facing is i have to delete the contents of last index of CTypedPtrArray.......am presenting you the code...
Code:
void CPflowView::OnDeleteBus()
{
// TODO: Add your command handler code here
CPflowDoc *pDoc=GetDocument();
ASSERT_VALID(pDoc);
CDeletebox delb;
delb.DoModal ();
int index=pDoc->GetNumBus();
int x;
while(index--)
{
if(pDoc->GetBus (index)->m_BusNo ==delb.m_no)
{
x=0;
break;
}
else
x=1;
}
if (x==0)
{
x=pDoc->GetNumBus();
for(int i=++index;i<=(x-1);i++)
{
pDoc->PutBus(pDoc->GetBus(index),(--index));
}
}
//want to delete the content of last index here
}
well here the PutBus function is used to shift the contents of CTypedPtrArray.........
-
June 3rd, 2009, 07:04 AM
#4
Re: regarding CTypedPtrArray,moving of array
can somebody tell me what is setatgrow function suppose to do.....
please help me....
thnx
-
June 3rd, 2009, 07:12 AM
#5
Re: regarding CTypedPtrArray,moving of array
It is not really clear what you are trying to do. There is a RemoveAt() member
function that removes an element in the collection at a specified index. (you still
need to take care of deallocating dynamicallt allocated memory)
-
June 3rd, 2009, 07:17 AM
#6
Re: regarding CTypedPtrArray,moving of array
can you please tell if i use RemoveAt function then how can i reduce the size of of CTypedPtrArray since am not allocating memory for it anywhere specifically.....
-
June 3rd, 2009, 07:23 AM
#7
Re: regarding CTypedPtrArray,moving of array
MFC will reduce the memory allocated by CTypedPtrArray object after you call RemoveAt. You, however, have to free the memory the pointer at the CTypedPtrArray position, you are removing at, points to (since you created your object with new you must now delete it using delete).
Besides, in the code snippet you have posted is absolutely unclear what CTypedPtrArray is and what type of objects you are creating (which pointers are supposed to be set at the CTypedPtrArray)
Victor Nijegorodov
-
June 3rd, 2009, 07:24 AM
#8
Re: regarding CTypedPtrArray,moving of array
I don't understand. You must be adding elements to the array at some time.
-
June 3rd, 2009, 08:04 AM
#9
Re: regarding CTypedPtrArray,moving of array
Maybe a simple example will help. I used a message box in the
example for demonstration purposes only ... you should use
a debugger to look at the results
Code:
struct CValue : public CObject
{
CValue(int val): value(val) {}
int value;
};
// ...
CTypedPtrArray<CPtrArray,CValue*> arr;
// add some initial elements to the colection
arr.Add(new CValue(0));
arr.Add(new CValue(1));
arr.Add(new CValue(2));
arr.Add(new CValue(3));
arr.Add(new CValue(4));
// set the value of arr[7] .. if necessary, the size of the collection
// will increase to 8. Any "skipped" index positions we be st to NULL
arr.SetAtGrow(7, new CValue(99) );
// free the dynamically allocated memory for the third element
// and remove it from the collection
delete arr[2];
arr.RemoveAt(2);
// loop thru the colection
for (int i=0; i<arr.GetCount(); ++i)
{
if (arr[i] != 0)
{
CString s;
s.Format(_T("%d"),arr[i]->value);
AfxMessageBox(s);
}
else
{
AfxMessageBox(_T("null pointer"));
}
}
// "messagebox output" = 0 , 1 , 3 , 4 , null pointer , null pointer , 99
// free all dynamically allocated memory and remove all elements from
// the collection
for (int i=0; i<arr.GetSize(); ++i)
{
delete arr[i];
}
arr.RemoveAll();
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|