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. pointers to objects in lists

pointers to objects in lists

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 3 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

    class classA { public: //uses push_back to add a new Object to objectList addObject(Object newObject); private: list objectList; }; class classB { private: classA* Aptr; void addObjectToClassA(); }; void ClassB::addObjectToClassA() { Object newObject(); Aptr->addObject(newObject); } Hello all, I have what is probably a simple question, but I've been wrestling with it for a while. I have code that looks like what I typed above. classA has an std::list of Objects. classB has a method that will add a new Object to that list in classA. As I understand it, it creates a copy of the Object and the copy is what is added to the list. How can I iterate over objectList and actually modify the objects in the list? When I iterate now it seems that the Objects in the list are not modified... It seems that copies are modified instead. I thought one option would be to make objectList a list of pointers to Objects instead... But what will happn when I add an object to the list in classB? If I create a local Object* and add that to the objectList, does the pointer get added or does a copy of the pointer get added? (I'm just worried that the pointer won't refer to anything after addObjectToClassA() exits). Thanks! -Jordan Atlas

    C 1 Reply Last reply
    0
    • J Jordan C Atlas

      class classA { public: //uses push_back to add a new Object to objectList addObject(Object newObject); private: list objectList; }; class classB { private: classA* Aptr; void addObjectToClassA(); }; void ClassB::addObjectToClassA() { Object newObject(); Aptr->addObject(newObject); } Hello all, I have what is probably a simple question, but I've been wrestling with it for a while. I have code that looks like what I typed above. classA has an std::list of Objects. classB has a method that will add a new Object to that list in classA. As I understand it, it creates a copy of the Object and the copy is what is added to the list. How can I iterate over objectList and actually modify the objects in the list? When I iterate now it seems that the Objects in the list are not modified... It seems that copies are modified instead. I thought one option would be to make objectList a list of pointers to Objects instead... But what will happn when I add an object to the list in classB? If I create a local Object* and add that to the objectList, does the pointer get added or does a copy of the pointer get added? (I'm just worried that the pointer won't refer to anything after addObjectToClassA() exits). Thanks! -Jordan Atlas

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      I don't believe that a copy is made when you add something to a list, but either way, if a pointer is copied, it still points to the same memory address. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

      J M 2 Replies Last reply
      0
      • C Christian Graus

        I don't believe that a copy is made when you add something to a list, but either way, if a pointer is copied, it still points to the same memory address. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

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

        Christian Graus wrote: but either way, if a pointer is copied, it still points to the same memory address. Yes, but if I have a function like this: void ClassB::addObjectToClassA() { Object* newObject; Aptr->addObject(newObject); } What happens after the function exits? Does the pointer point to anything (sensible) anymore? Thanks, -Jordan Atlas

        C 1 Reply Last reply
        0
        • J Jordan C Atlas

          Christian Graus wrote: but either way, if a pointer is copied, it still points to the same memory address. Yes, but if I have a function like this: void ClassB::addObjectToClassA() { Object* newObject; Aptr->addObject(newObject); } What happens after the function exits? Does the pointer point to anything (sensible) anymore? Thanks, -Jordan Atlas

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          No, it never pointed to anything sensible. Object * newObject = new Object(); NOW you have a pointer to something sensible, and you can make as many copies of that pointer as you like. Until you put delete newObject; it will continue to exist, and if you fail to ever call delete, you've got a memory leak. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

          1 Reply Last reply
          0
          • C Christian Graus

            I don't believe that a copy is made when you add something to a list, but either way, if a pointer is copied, it still points to the same memory address. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

            M Offline
            M Offline
            markkuk
            wrote on last edited by
            #5

            If a STL list contains objects instead of pointers to objects, then objects added to the list get copied, just like with all other STL containers.

            C 1 Reply Last reply
            0
            • M markkuk

              If a STL list contains objects instead of pointers to objects, then objects added to the list get copied, just like with all other STL containers.

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              Yeah, I realised after commenting that I'm too used to C# now. It's still the case that if you have an iterator, you can modify the object, and the object in the container will be modified, because you have a reference to the object in the container, right ? Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

              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