class Plot {
int nx; // number of "x" values
int ny; // number of "y" values
int nz; // number of "z" values
double* grid; // one dimensional array which stores nx*ny*nz values
double tgrid(int ix, int iy, int iz); // function which searches grid and returns value
public:
/* default constructor */
Plot() : nx(0), ny(0), nz(0), grid(NULL) { }
/* rule of five copy constructor */
Plot(const Plot& b) : nx(b.nx), ny(b.ny), nz(b.nz) {
int max = nx*ny*nz;
if (max) {
grid = new double(max);
for(int i=0; i<max; ++i)
grid[i] = b.grid[i];
} else
grid = NULL;
}
/* rule of five move constructor */
Plot(Plot&& b) : nx(b.nx), ny(b.ny), nz(b.nz) grid(b.grid) { b.grid = NULL; }
/* load constructor */
Plot(std::istream& b) : nx(0), ny(0), nz(0), grid(NULL) { Load(b); }
/* rule of five destructor */
~Plot() { delete[] grid; }
/* rule of five assignment operator */
Plot& operator=(const Plot& b) {
int max = b.nx*b.ny*b.nz;
double* t = new double[max];
for(int i=0; i<max; ++i)
t[i] = b.grid[i];
//all exceptions above this line, NOW we can alter members
nx = b.nx;
ny = b.ny;
nz = b.nz;
delete [] grid;
grid = t;
}
/* rule of five move operator */
Plot& operator=(Plot&& b) {
nx = b.nx;
ny = b.ny;
nz = b.nz;
delete [] grid;
grid = b.grid;
b.grid = NULL;
}
/* always a good idea for rule of five objects */
void swap(const Plot& b) {
std::swap(nx, b.nx);
std::swap(ny, b.ny);
std::swap(nz, b.nz);
std::swap(grid, b.grid);
}
/* your load member */
void Load(std::istream& in) {
//load data
//all exceptions above this line, NOW we can alter members
//alter members
};
};
int main() {
Plot x; //default constructor allocates no memory
Plot y(x); //allocates no memory, because x has no memory
Plot z(std::cin); //loads from stream, allocates memory
x = z; //x.grid is _now_ given a value besides NULL.
}
Это отвечает на ваши вопросы, я думаю. Еще: используйте std :: vector.