Jump to content
  • Advertisement
Sign in to follow this  
Raymondo

Deep Copying Confusion w/ C++

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

Hey guys, I have a question regarding proper implementation deep copy for C++. Note: Some of you may consider this an beginner question, so accept my apologies in advance and move it if needed. I'm having trouble understanding how to actually *get* deep copying to work with my copy constructor. Here's the situation: I have a class, myClass. Let's say for simplicities sake, that it contains only one member, a private data member. This private data member is an Array of pointers to objects of myOtherClass. So we have:
class myClass
{
public:

   myClass();
   myClass(const myClass&);
   myClass& operator = (const myClass&);
   ~myClass();

private:

   myOtherClass* theArray[MAX_SIZE];

};

//************

myClass::myClass(const myClass& src)
{

   for (int i=0;i<MAX_SIZE;i++)
      theArray = src.theArray //This doesn't seem correct...

}
How exactly do I go about making sure, in the constructor (and in the assignment = override), that the array is copied properly? That is, that the OBJECTS that theArray points to are copied. I'm obviously missing something here in terms of understanding... TIA

Share this post


Link to post
Share on other sites
Advertisement
Do the pointers point at single instances or arrays? If it's just instances, you need to allocate new instances using `new'.

   for (int i=0;i<MAX_SIZE;i++)

theArray = new myOtherClass(*src.theArray);




Remember that besides the assignment operator and the copy constructor, you'll have to also provide a destructor.

Share this post


Link to post
Share on other sites
The pointers are pointing to single instances.

Your suggestion makes sense to me. So, if perhaps, myOtherClass only were to contain standard types, like double or int, the default constructor can be just be used, right?

Share this post


Link to post
Share on other sites
Quote:
Original post by Raymondo
The pointers are pointing to single instances.

Your suggestion makes sense to me. So, if perhaps, myOtherClass only were to contain standard types, like double or int, the default constructor can be just be used, right?


Yes, you only need to worry about deep copying if objects hold pointers to data they own.

Why are you using an array of pointers instead of an array of objects?

Share this post


Link to post
Share on other sites
I'm writing a game of chess, and my piece class doesn't have a default constructor. I'm using pointers *mainly* because it allows me to defer the invocation of a constructor for each element.

Share this post


Link to post
Share on other sites
By the way, static arrays are deep copied by the default copy constructor or assignment operator. Usually you don't need any of the big three (copy constructor, destructor, assignment operator) unless you call new/delete manually. Even if you need your "theArray" member to be variable size, you can use std::vector, which eliminates the need to define a copy constructor or destructor.

Share this post


Link to post
Share on other sites
For some reason, I'm seg-faulting when I call the copy constructor from main?

When I simplify the constructor to:

theArray[0][0] = NULL;


it still seg-faults.



Here is what I have currently:


//****MAIN.CPP*****

myClass fun1;

myClass fun2(fun1);


//****myClass.CPP*****

myClass::myClass(const myClass& src)
{

int row, column;

for (row = 0; row < MAX_SIZE; row++)
{
for (column = 0; column < MAX_SIZE; column++)
{
theArray[row][column] = new myOtherClass(*src.theArray[row][column]);
}
}
}

Share this post


Link to post
Share on other sites
What does your declaration of theArray look like? In your original post it's a 1D array, but in your most recent one, it seems to be a 2D array.

If your array declaration is:
OtherClass* theArray[MAX_SIZE][MAX_SIZE];

your code works fine for me in my test project...

But
OtherClass* theArray[][MAX_SIZE];

or
OtherClass** theArray[MAX_SIZE];

will crash it due to out-of-bounds and uninitialized pointer use, respectively.



EDIT: Another potential problem crossed my mind, which I'd left out of my test project.

You said that you were deferring the creation of the array elements? In your copy-constructor, you're dereferencing all your elements in order to call myOtherObject's copy constructor. But if you have NULL or garbage array elements in theArray being copied, you're going to have a crash.

Share this post


Link to post
Share on other sites
Quote:
Original post by taz0010
By the way, static arrays are deep copied by the default copy constructor or assignment operator.


Well, depends on your definition of deep-copy. If the static array contains pointers to objects (say, an array of strings), just the pointers of the static array are copied (shallow-copy). For there to be deep-copy, you must iterate over all elements, and reallocate / memcpy.

Dynamic arrays, and I think this is what you mean, are not copied all elements automatically. Just it's pointer. While static arrays do get their elements copied, by default.

To put it generally; when pointers come in, you must worry about shallow or deep copying.

Share this post


Link to post
Share on other sites
If you replace your array of pointers with std::vector<std::vector<myOtherClass> > or boost::multi_array<myOtherClass, 2>, you don't need to supply your own copy constructor, assigment operator and destructor anymore, because the default versions will do exactly what you want.

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.

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!