Jump to content
  • Advertisement
Sign in to follow this  
freeworld

copy a pointed to variable while making a new one.

This topic is 3080 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);


Share this post


Link to post
Share on other sites
Advertisement
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);

Share this post


Link to post
Share on other sites
Quote:
Original post by alvaro
You 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?

Share this post


Link to post
Share on other sites
Quote:
Original post by freeworld
Quote:
Original post by alvaro
You 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.

Share this post


Link to post
Share on other sites
// 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 this
MyVar* v2 = new MyVar;
v2 = v1; // memory leakage, small in this case but relevant :P


Uhhh almost forgot, there would be a copy constructor unless you explicitly declare it private/protected... So.. meh I donno...

Share this post


Link to post
Share on other sites
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 vector

MyVar 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
// properly

v1 = 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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
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 vector

MyVar 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
// properly

v1 = 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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!