   copy a pointed to variable while making a new one.
  I was wandering if it was possible to do this, just because I'm not sure how copying std::vectors works or if it does.class MyVar {std::vector vector;};/* create a new instance of MyVar */MyVar* v1 = new MyVar;/* create another instance but copy over the contents of the first one */MyVar* v2 = new MyVar;(*v2) = (*v1);
  You could test it first... But yes, that works.Even better would be to construct the new object as a copy to begin with:MyVar* v2 = new MyVar(*v1);;Quote:Original post by alvaroYou could test it first... But yes, that works.Even better would be to construct the new object as a copy to begin with:*** Source Snippet Removed ***and that won't just copy the pointer ie they won't point to the same variable?;Quote:Original post by freeworldQuote:Original post by alvaroYou could test it first... But yes, that works.Even better would be to construct the new object as a copy to begin with:*** Source Snippet Removed ***and that won't just copy the pointer ie they won't point to the same variable?No, or I wouldn't have posted it. And again, you can test these things on your own, and you'll probably learn a few things by getting in the habit of testing your understanding of the language using a compiler.   ;// this may be safer:MyVar* v2 = new MyVar(*v1) // obviously you need to provide a constructor. best make it explicit to.// As you may trip yourself up with something like thisMyVar* v2 = new MyVar;v2 = v1; // memory leakage, small in this case but relevant :PUhhh almost forgot, there would be a copy constructor unless you explicitly declare it private/protected... So.. meh I donno...   ;  Aargh.Don't use pointers without a reason.std::vector has been very carefully designed so that in terms of copying, letting things fall out of scope etc. you can use it just like any primitive (e.g. int).So all you need is:MyVar v1; // calls the default constructor, // which in turn calls the default constructor for the vectorMyVar v2 = v1; // calls the copy constructor, which (since you haven't defined one)// copies each data member with its copy constructor - i.e. copies the vector// properlyv1 = v2; // calls the assignment operator, which likewise assigns each member// since you haven't defined your own.Don't make things any more complicated than necessary. Not even a little bit. Seriously. Your bug frequency will go way down. Trust me.   ;Quote:Original post by ZahlmanAargh.Don't use pointers without a reason.std::vector has been very carefully designed so that in terms of copying, letting things fall out of scope etc. you can use it just like any primitive (e.g. int).So all you need is:MyVar v1; // calls the default constructor, // which in turn calls the default constructor for the vectorMyVar v2 = v1; // calls the copy constructor, which (since you haven't defined one)// copies each data member with its copy constructor - i.e. copies the vector// properlyv1 = v2; // calls the assignment operator, which likewise assigns each member// since you haven't defined your own.Don't make things any more complicated than necessary. Not even a little bit. Seriously. Your bug frequency will go way down. Trust me.Perhaps the OP knows this, but is curious as to how std::vector performs particular functions? I myself, would promote this kind of curiosity, it exposes the programmer to the "meat" of higher level functionality lest he be bound to high level trickery without any real knowledge of 'how' stuff works :P (its all part of the hobby). IMHO   