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. Managed C++/CLI
  4. vector problem

vector problem

Scheduled Pinned Locked Moved Managed C++/CLI
graphicshelpquestion
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.
  • P Offline
    P Offline
    Pazzuzu
    wrote on last edited by
    #1

    Hi all, When Iam making a "push_back" into the vector,Its pushing back a new object into my vector(co's it goes to the constructor of the object) although ,Iam passing a refererence of the object into the "push_back". Does "Push_Back" make a copy of the object passed to it.How can I avoid it.... Thanks...

    L 1 Reply Last reply
    0
    • P Pazzuzu

      Hi all, When Iam making a "push_back" into the vector,Its pushing back a new object into my vector(co's it goes to the constructor of the object) although ,Iam passing a refererence of the object into the "push_back". Does "Push_Back" make a copy of the object passed to it.How can I avoid it.... Thanks...

      L Offline
      L Offline
      Laffis
      wrote on last edited by
      #2

      Yeap push back will make a copy. It does not matter you pass a ref into it. You should have found that a lot of copy constructors take a reference and that's what you r doing. To avoid it, you should declare a vector of pointers rather than the that class. e.g. class CMyClass { .... }; std::vector<*CMyClass> myVec; Just remember to delete everyting at an appropriate time so that there's no memory leak

      T 1 Reply Last reply
      0
      • L Laffis

        Yeap push back will make a copy. It does not matter you pass a ref into it. You should have found that a lot of copy constructors take a reference and that's what you r doing. To avoid it, you should declare a vector of pointers rather than the that class. e.g. class CMyClass { .... }; std::vector<*CMyClass> myVec; Just remember to delete everyting at an appropriate time so that there's no memory leak

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        Laffis wrote: Yeap push back will make a copy. It does not matter you pass a ref into it. no, it get what you give to it, a ref, a pointer or a value, but nowhere push_back() take the decision of copying its parameter to add to the vector. Laffis wrote: You should have found that a lot of copy constructors take a reference and that's what you r doing. but it's their job ! constructors are supposed to create a new instance of a class, getting some or some other parameter types. of course, a constructor getting a reference have to copy/clone its parameter to avoid multiple references on the same memory area...


        TOXCCT >>> GEII power
        [toxcct][VisualCalc]

        L 1 Reply Last reply
        0
        • T toxcct

          Laffis wrote: Yeap push back will make a copy. It does not matter you pass a ref into it. no, it get what you give to it, a ref, a pointer or a value, but nowhere push_back() take the decision of copying its parameter to add to the vector. Laffis wrote: You should have found that a lot of copy constructors take a reference and that's what you r doing. but it's their job ! constructors are supposed to create a new instance of a class, getting some or some other parameter types. of course, a constructor getting a reference have to copy/clone its parameter to avoid multiple references on the same memory area...


          TOXCCT >>> GEII power
          [toxcct][VisualCalc]

          L Offline
          L Offline
          Laffis
          wrote on last edited by
          #4

          push_back should make a copy toxcct, it being a copy of the object, or a copy of a pointer or whatever you declare it should be. Consider the following test code: class CMy { public: CMy(const CMy &m) { this->n = m.n; }; CMy(const CMy *pm) { this->n = pm->n; }; operator=(const CMy &m) { this->n = m.n; }; CMy() {n=911;}; int n; }; void main(...) { std::vector vt; CMy *my1 = new CMy(); vt.push_back(*my1); // made a copy here? delete my1; // delete the created CMy object. std::vector::iterator it = vt.begin(); // LineA,crash? CMy my2 = *it; // LineB,crash? int nn = my2.n; // But you still get 911 here! } If push_back does not make a new copy, LineA and LineB will crash. But on the contrary, you should see that at the last line the object still exists in the vector. It is not their decision to implement copy constructor. You should make your copy constructor when shallow copying is not enough, otherwide do not making your own. The decision is yours. In the above test code the copy constructors are not neccesary at all. I had the impression that the msg author has debugged into copy constructor so I assumed he wrote his own version.

          P 1 Reply Last reply
          0
          • L Laffis

            push_back should make a copy toxcct, it being a copy of the object, or a copy of a pointer or whatever you declare it should be. Consider the following test code: class CMy { public: CMy(const CMy &m) { this->n = m.n; }; CMy(const CMy *pm) { this->n = pm->n; }; operator=(const CMy &m) { this->n = m.n; }; CMy() {n=911;}; int n; }; void main(...) { std::vector vt; CMy *my1 = new CMy(); vt.push_back(*my1); // made a copy here? delete my1; // delete the created CMy object. std::vector::iterator it = vt.begin(); // LineA,crash? CMy my2 = *it; // LineB,crash? int nn = my2.n; // But you still get 911 here! } If push_back does not make a new copy, LineA and LineB will crash. But on the contrary, you should see that at the last line the object still exists in the vector. It is not their decision to implement copy constructor. You should make your copy constructor when shallow copying is not enough, otherwide do not making your own. The decision is yours. In the above test code the copy constructors are not neccesary at all. I had the impression that the msg author has debugged into copy constructor so I assumed he wrote his own version.

            P Offline
            P Offline
            Pazzuzu
            wrote on last edited by
            #5

            Yes,I did Overrided my copy Constructor,co's wanted deep copying.Now Iam storing in my Vector- Pointers & not objects anymore....

            L 1 Reply Last reply
            0
            • P Pazzuzu

              Yes,I did Overrided my copy Constructor,co's wanted deep copying.Now Iam storing in my Vector- Pointers & not objects anymore....

              L Offline
              L Offline
              Laffis
              wrote on last edited by
              #6

              Just becareful with memory leak and you are rolling. As far as I know, list, map also make copy of inserted element.

              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