problem when moving objects to temp array

Hello,
Lets say that I have two classes, the first called Y and the second called X.
----------------------------------------------------------------------
class Y
{
public:
Y(const Y &a) //copy constructor
.
.
.
}
----------------------------------------------------------------------
#include "Y.h"
Class X
{
public:
.
.
.

private:
Y** Arr;
unsigned Arr_size;
}
----------------------------------------------------------------------
As You see, X got in private array of pointers to Y objects.

Now somewhere in the code I want to move all the objects from Arr to temp (temp is the same type as Arr, made it by using new as Arr, also temp and Arr got the same size) and delete the Arr. So I wrote this:
----------------------------------------------------------------------
for (unsigned i=0;i<Arr_size;i++)
{
temp[i]=Arr[i];
delete Arr[i];
}
delete [] Arr;


I noticed that theres a problem after the "delete Arr[i];", when I try to print temp[i] before the "delete Arr[i];" I get the exact data that were in Arr[i], however if I try to print temp[i] after the "delete Arr[i];" command I get Access Violation error.

how can I fix this?

Last edited on
First, understand what you are doing.

1
2
3
4
5
temp[i]=Arr[i];
delete Arr[i];
//is equivalent to 
temp[i]=Arr[i];
delete temp[i];



Then, abstract a little.
1
2
3
class X{
   some_container<Y> Arr; //by instance std::vector
};



PS: ¿why are you writing `Class' (uppercase)?
Topic archived. No new replies allowed.