vector problem
-
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...
-
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...
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
-
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
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] -
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]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.
-
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.
-
Yes,I did Overrided my copy Constructor,co's wanted deep copying.Now Iam storing in my Vector- Pointers & not objects anymore....