Jump to content
  • Advertisement
Sign in to follow this  
rolfm

A Good way of doing deep copy in c++?

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

So I'm developing this hobby game engine for a 2D platformer game. When making new objects in the game(for example while loading a level) "prototype"-objects are copied to the game space and some variables are modified as needed(like position). I need this copying to be fast but I also prioritize development time/readability of code.

So i came up with these two different methods in class A, copy1 and copy2.

class A{
public:

A(int na, int nb, int nc){
a = na;
b = nb;
c = nc;
}

A(){
a = -1;
b = -2;
c = -3;
}


A * copy1(){// specified deep copy
A * clone = new A;
//takes what i need and puts it in the copy
clone->a = a;
clone->b = b;
clone->c = c;
return clone;
}
A * copy2(){ // Stack-copy-like copy
A * clone = new A;
*clone = *this; //Makes the compiler do it
return clone;

}

void print_data(){
std::cout << a << " " << b << " " << c << "\n";
}


private:
//the variables
int a;
int b;
int c;
int d;
int e;


};


//Testing
int main(){
// setup object
A test = A(-5,-3,5);

// Create copies
A * test2 = test.copy1();
A * test3 = test.copy2();

//overwrite initial object
test = A();

//Write out data
test.print_data();
test2->print_data();
test3->print_data();
}



This outputs:

-1 -2 -3
-5 -3 5
-5 -3 5

Which proves that the copying went as expected. As I see it copy2 is easiest to implement as I don't need to specify which variables to copy, but is copy1 faster? Are there any other pros or cons? Are there better ways to copy objects?

Share this post


Link to post
Share on other sites
Advertisement
The most natural (and probably fastest) way would be to simply use the copy constructor ... I mean after all that's what it's there for.


int main()
{
A test( -5, -3, 5 );
A copy( test );

return 0;
}



Incidentally, you've unknowingly used the copy constructor anyway in the line:


A test = A(-5,-3,5);



A(-5,-3,5) creates a temporary, and although it looks as if the assignment operator was used to copy the value of that temporary into "test", in reality "test" is actually copy constructed from the temporary created by A(-5,-3,5).

Share this post


Link to post
Share on other sites
As long as you adhere to the rule of three and proper RAII, you should be able to use copy construction and rely on the compiler's default implementations to fill in the missing pieces:

A* clone() const { return new A(*this); }

Share this post


Link to post
Share on other sites
Quote:
Original post by Zipster
As long as you adhere to the rule of three and proper RAII, you should be able to use copy construction and rely on the compiler's default implementations to fill in the missing pieces:

A* clone() const { return new A(*this); }


Though in C++ cloning does not require operator new.

As a general rule, if all your members obey the Rule of Three or are of builtin type (which are copyiable), you don't need to write your own copy-mechanisms:

class Foo {
int a, b;
bool c;
public:
Foo (int a, int b, bool c) : a(a), b(b), c(c) {}
};

int main () {
Foo alpha (1,2, false);
Foo bravo = alpha;
}


Copy-Ctor and Copy-Assignment are emitted automatically by the compiler, as all members of Foo have well-defined copying.

Share this post


Link to post
Share on other sites
Ah. Didn't think a copy constructor was created by default. Thanks for the great answers. [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by Zipster
Quote:
Original post by phresnel
Though in C++ cloning does not require operator new.

If by "cloning" you really mean "deep copying", then no. But if by "cloning" you really mean "virtual construction", then yes.


Never thought about copying objects through a pointer-to-base, good point. [edit: wanted to rate++, but did already :P]

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!