A pointer delimma (requesting help from pointer/dynamic array gurus)
-
Ok, I have a CArray container like so:
CArray objectList;
and I declared a dynamic array in a separate class like so:class SelectionBox { public: //constructor , destructor, etc... void AddContent(myObject *); // <-- this is what's I can't figure out protected: myObject *content; // to be used as dynamic array unsigned long numContents; // counter that contains the number // of contents in the selection box }
So now I built my AddContent() function like this:void SelectionBox::AddContent(myObject *c) { unsigned long counter; myObject *tempObject; tempObject = new myObject[numContents]; tempObject = content; content = new myObject[numContents + 1]; for(counter = 0; counter < numContents; counter++) { content[counter] = tempObject[counter]; } //c->IsSelected = true; //<- this gives me the illusion that it's working content[numContents] = c; //content[numContents].IsSelected = true; // <- this is what I need to work numContents++; delete [] tempObject; }
Finally, in another part of the program, I shoot the address of one of the objects stored in the CArray to the AddContent function. At first the function appeared to be working.. That is until I later tried to manipulate the original objects in the CArray by manipulating what I thought was the pointer to them in my SelectionBox class. Then I found out that I had copies of data stored and not a pointer to the CArray data. I can't for the life of me figure out how to declare the function and pass the data so that I can store an array of pointers/addresses to the data in the CArray container. Can someone show me an example, or maybe a link to a relevant tutorial? -
Ok, I have a CArray container like so:
CArray objectList;
and I declared a dynamic array in a separate class like so:class SelectionBox { public: //constructor , destructor, etc... void AddContent(myObject *); // <-- this is what's I can't figure out protected: myObject *content; // to be used as dynamic array unsigned long numContents; // counter that contains the number // of contents in the selection box }
So now I built my AddContent() function like this:void SelectionBox::AddContent(myObject *c) { unsigned long counter; myObject *tempObject; tempObject = new myObject[numContents]; tempObject = content; content = new myObject[numContents + 1]; for(counter = 0; counter < numContents; counter++) { content[counter] = tempObject[counter]; } //c->IsSelected = true; //<- this gives me the illusion that it's working content[numContents] = c; //content[numContents].IsSelected = true; // <- this is what I need to work numContents++; delete [] tempObject; }
Finally, in another part of the program, I shoot the address of one of the objects stored in the CArray to the AddContent function. At first the function appeared to be working.. That is until I later tried to manipulate the original objects in the CArray by manipulating what I thought was the pointer to them in my SelectionBox class. Then I found out that I had copies of data stored and not a pointer to the CArray data. I can't for the life of me figure out how to declare the function and pass the data so that I can store an array of pointers/addresses to the data in the CArray container. Can someone show me an example, or maybe a link to a relevant tutorial?I recommend you use CTypedPtrArray in your class rather than try to manage your own array. There are other options to be sure but that should suffice. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_Collections_Topics.asp[^]
"No matter where you go, there your are." - Buckaroo Banzai
-pete
-
I recommend you use CTypedPtrArray in your class rather than try to manage your own array. There are other options to be sure but that should suffice. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_Collections_Topics.asp[^]
"No matter where you go, there your are." - Buckaroo Banzai
-pete