Computational Physics - Department of Physics

(Axel Boer) #1

6.7 A vector and matrix class 199


dx2 = dx1*size[1];
dx3 = dx2*size[2];
dx4 = dx3*size[3];
dx5 = dx4*size[4];
}catch(std::bad_alloc&){
std::cerr <<"Array::allocate -- unable to allocate array of length "<< length <<
std::endl;
exit(1);
}
}
}
public:

/**
*@brief Constructor with default arguments.
*
*Creates an array with one or two-dimensions.
*
*@param int nrows. Number of rows in the array.
*@param int ncolsd. Number of columns in the array.
**/
Array(intni=0,intnj=0,intnk=0,intnl=0,intnm=0,intnn=0){
// Allocate memory
allocate(ni,nj,nk,nl,nm,nn);
}// end constructor

//! Constructor
Array(T*array,intndim_,intsize_[]){
ndim = ndim_;
length = 1;
inti;
for(i=0; i<ndim; i++){
size[i] = size_[i];// Copy only the ndim entries. The rest is zero by default.
length *= size[i];
}
// Now when we known the length, we should not forget to allocate memory!!!!
data =newT[length];
// Copy the entries from array to data:
for(i=0; i<length; i++){
data[i] = array[i];
}
}// End constructor.

//! Copy constructor
Array(constArray<T>& array);
//! Destructor
~Array();
Free download pdf