Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Rudan

Copy an instance of a class

This topic is 5827 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

Let''s say I have a created an object from a class. Now I want to copy this object. I tried to use memcpy but it didn''t work. I think it failed because the class has inherited from another class. Anyway, I bet that this is really easy to do, but I''ve yet to find the answer...

Share this post


Link to post
Share on other sites
Advertisement
Just use a simple assignment "a = b;". Of course, if your class contains pointers to dynamically allocated memory or references, you''ll most likely need overload the assignment operator.

Share this post


Link to post
Share on other sites
That will only give me a pointer that points to the same object. The problem is that I need to delete the old object after I have copied it.

Share this post


Link to post
Share on other sites
quote:
Original post by Rudan
That will only give me a pointer that points to the same object.

Not so. It will copy the identity of the object from b to a. If the objects happen to be pointers, then that''s what will be copied. If you are really talking about copying pointee objects, then you need to dereference the pointers... *a = *b
quote:

The problem is that I need to delete the old object after I have copied it.

Then delete the object.

Share this post


Link to post
Share on other sites
I guess I''m not making much sense, here.

This is what I want to do:
I have two pointers. One points to an object. I want to copy this object and make the other pointer point to the copy.

My first explanation wasn''t very good. Sorry about that.

Share this post


Link to post
Share on other sites
quote:
Original post by Rudan
I have two pointers. One points to an object. I want to copy this object and make the other pointer point to the copy.

You mean like this...


int *p1 = new int(5);
int *p2 = new int(*p1);


?

Share this post


Link to post
Share on other sites
quote:
int *p1 = new int(5);
int *p2 = new int(*p1);


I didn''t know you could do that with a class object, but it seems to work. Thanks.

Share this post


Link to post
Share on other sites
Be aware that, if your class contains pointers to dynamically allocated memory, you''ll need a copy constructor for this to work.

Share this post


Link to post
Share on other sites
Also, with polymorphic types you'll need a separate virtual clone()-function to avoid slicing.

    
class B {
virtual B* clone() const {
return new(*this);
}
...
virtual ~B() {}
}

class D : public B {
virtual D* clone() const {
return new(*this);
}
...
}

int main() {
B* b1 = new D
B* b2 = b1->clone();
}



[edited by - civguy on December 4, 2002 3:26:23 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!