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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Pointer to an object in a std::list

Pointer to an object in a std::list

Scheduled Pinned Locked Moved C / C++ / MFC
question
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.
  • J Offline
    J Offline
    Jordan C Atlas
    wrote on last edited by
    #1

    Hello, I'd like to loop over a std::list and for a given element in the list, get a pointer to that element. My code looks something like this: Object* pointerToObject; iteratorType listStart, listEnd, listIt; list objectList = getObjectList(); listStart = objectList.begin(); listEnd = objectList.end(); for(listIt = listStart; listIt != listEnd; listIt++) { pointerToObject = listIt; //This code won't compile --> but it illustrates my intention... I want to get a pointer to an object in the list... } Any ideas? Thanks! -Jordan Atlas

    PJ ArendsP 1 Reply Last reply
    0
    • J Jordan C Atlas

      Hello, I'd like to loop over a std::list and for a given element in the list, get a pointer to that element. My code looks something like this: Object* pointerToObject; iteratorType listStart, listEnd, listIt; list objectList = getObjectList(); listStart = objectList.begin(); listEnd = objectList.end(); for(listIt = listStart; listIt != listEnd; listIt++) { pointerToObject = listIt; //This code won't compile --> but it illustrates my intention... I want to get a pointer to an object in the list... } Any ideas? Thanks! -Jordan Atlas

      PJ ArendsP Offline
      PJ ArendsP Offline
      PJ Arends
      wrote on last edited by
      #2

      pointerToObject = &(*listIt);

      I think that should work. What I usually do is have the list store the pointers instead of the objects themselves. The list makes a copy of everything it stores so storing pointers means it only has to copy 4 bytes for each object while storing the object themselves means the list copies the entire object.

      list<* object> objectList;


      "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!

      Within you lies the power for good; Use it!

      J 1 Reply Last reply
      0
      • PJ ArendsP PJ Arends

        pointerToObject = &(*listIt);

        I think that should work. What I usually do is have the list store the pointers instead of the objects themselves. The list makes a copy of everything it stores so storing pointers means it only has to copy 4 bytes for each object while storing the object themselves means the list copies the entire object.

        list<* object> objectList;


        "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!

        J Offline
        J Offline
        Jordan C Atlas
        wrote on last edited by
        #3

        PJ Arends wrote: pointerToObject = &(*listIt); Hey PJ, This code works! Thanks! I usually store pointers in the lists as well, but for some reason I didn't in this case. I'll have to look at the code to see why I implemented it that way. Merci, -Jordan Atlas

        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