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. STL Containers store a copy?

STL Containers store a copy?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++docker
6 Posts 4 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.
  • P Offline
    P Offline
    piul
    wrote on last edited by
    #1

    Hello! My question is, do STL containers store a copy or they keep the object itself? Hence, can I insert a local variable into a container?

    typedef struct{
    int number;
    string name; } myData;
    class myClass{
    void InsertData(myData &a);
    void oneFunction ();
    void otherFunction ();
    collection m_memberCollection;
    };
    void oneFunction() {
    myData localData;
    localData.name = "Paul";
    InsertData(localData);
    }
    void myClass::InsertData (myData &a){
    a.number = 5;
    m_memberCollection.push_back(a);
    }
    void otherFunction (){
    printf ("%s %d", m_memberCollection[0].name.c_str(), m_memberCollection[0].number);
    }

    Or do I need to use a collection of the type collection<*myData>?

    L A 2 Replies Last reply
    0
    • P piul

      Hello! My question is, do STL containers store a copy or they keep the object itself? Hence, can I insert a local variable into a container?

      typedef struct{
      int number;
      string name; } myData;
      class myClass{
      void InsertData(myData &a);
      void oneFunction ();
      void otherFunction ();
      collection m_memberCollection;
      };
      void oneFunction() {
      myData localData;
      localData.name = "Paul";
      InsertData(localData);
      }
      void myClass::InsertData (myData &a){
      a.number = 5;
      m_memberCollection.push_back(a);
      }
      void otherFunction (){
      printf ("%s %d", m_memberCollection[0].name.c_str(), m_memberCollection[0].number);
      }

      Or do I need to use a collection of the type collection<*myData>?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      piul wrote:

      Or do I need to use a collection of the type collection<*myData>?

      That would be a collection of pointers, which would probably lead to memory leaks and/or loss of information. Your original code takes copies of the objects and adds them to the collection, so the data is retained.

      speaking as ...

      P 1 Reply Last reply
      0
      • L Lost User

        piul wrote:

        Or do I need to use a collection of the type collection<*myData>?

        That would be a collection of pointers, which would probably lead to memory leaks and/or loss of information. Your original code takes copies of the objects and adds them to the collection, so the data is retained.

        speaking as ...

        P Offline
        P Offline
        piul
        wrote on last edited by
        #3

        thanks!

        1 Reply Last reply
        0
        • P piul

          Hello! My question is, do STL containers store a copy or they keep the object itself? Hence, can I insert a local variable into a container?

          typedef struct{
          int number;
          string name; } myData;
          class myClass{
          void InsertData(myData &a);
          void oneFunction ();
          void otherFunction ();
          collection m_memberCollection;
          };
          void oneFunction() {
          myData localData;
          localData.name = "Paul";
          InsertData(localData);
          }
          void myClass::InsertData (myData &a){
          a.number = 5;
          m_memberCollection.push_back(a);
          }
          void otherFunction (){
          printf ("%s %d", m_memberCollection[0].name.c_str(), m_memberCollection[0].number);
          }

          Or do I need to use a collection of the type collection<*myData>?

          A Offline
          A Offline
          Aescleal
          wrote on last edited by
          #4

          Not particularly pertinent to the question but just a quick note... You don't need to write typedef structs in C++ the way you do in C to introduce a new type. Instead you can just write:

          struct myData
          {
          // Members
          };

          and it'll do almost the same thing as your typedef (try adding some member functions to see what the difference is).

          S L 2 Replies Last reply
          0
          • A Aescleal

            Not particularly pertinent to the question but just a quick note... You don't need to write typedef structs in C++ the way you do in C to introduce a new type. Instead you can just write:

            struct myData
            {
            // Members
            };

            and it'll do almost the same thing as your typedef (try adding some member functions to see what the difference is).

            S Offline
            S Offline
            Stefan_Lang
            wrote on last edited by
            #5

            I wonder how many people will catch that hint, so here goes: The only difference is that the typedef'd struct is unnamed, so you cannot add a constructor or destructor! You could fix that by adding a name explicitely:

            typedef struct myData_struct {
            ...
            } myData;

            But of course then you wouldn't need the typedef at all since myData would be synonymous to myData_struct.

            1 Reply Last reply
            0
            • A Aescleal

              Not particularly pertinent to the question but just a quick note... You don't need to write typedef structs in C++ the way you do in C to introduce a new type. Instead you can just write:

              struct myData
              {
              // Members
              };

              and it'll do almost the same thing as your typedef (try adding some member functions to see what the difference is).

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Absolutely right, but for those of us with K&R hardwired into the brain it's a tough habit to kick.

              speaking as ...

              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