It's better to use a vector
like woz suggested in the comment. But with array you can do this. But first you need to be sure who create the array and it should be that same file/class that delete it. A safe way would be to dont expose the original array and get access to it by using a function (be aware that this code is not thread safe).
class Array2D
{
public:
Array2D(int xSize, int ySize)
{
xS = xSize;
yS = ySize;
arr = new double*[xSize];
for(int i = 0; i < xSize; ++i)
arr[i] = new double[ySize];
}
bool GetData(int x, int y, double& value)
{
if(x < xS && y < yS)
{
value = arr[x][y];
return true;
}
return false;
}
bool SetData(int x, int y, double value)
{
if(x < xS && y < yS)
{
arr[x][y] = value;
return true;
}
return false;
}
~Array2D()
{
for (int i = 0; i < xS; i++)
{
delete [] arr[i];
}
delete [] arr;
}
private:
//A default constructor here will prevent user to create a no initialized array
Array2D(){};
double** arr;
int xS;
int yS;
};