Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. A pointer delimma (requesting help from pointer/dynamic array gurus)

A pointer delimma (requesting help from pointer/dynamic array gurus)

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialdockerdata-structureshelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NTense
    wrote on last edited by
    #1

    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?

    P 1 Reply Last reply
    0
    • N NTense

      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?

      P Offline
      P Offline
      palbano
      wrote on last edited by
      #2

      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

      N 1 Reply Last reply
      0
      • P palbano

        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

        N Offline
        N Offline
        NTense
        wrote on last edited by
        #3

        That worked perfectly!!!! Thanks Pete.. I didn't even know that container existed, .. which will help a quite a few more problems I've been facing. Thanks Again :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups