I made two custom classes: a Vector class(algebraic one, not std::vector), and a Matrix class. I easily implemented Vector class as a array of doubles, i.e i store entries like this:
double *tab = nullptr;
//in constructors
//tab = new double[size]{};
I have a problem with Matrix class though. It is supposed to be a array of pointers to Vectors, i.e:
Vector** tab = nullptr;
//dimensions
int m, n;
What if i simply do not implement it this way, and just do a array of Vectors rather than pointers?
Vector* tab = nullptr
//...
//destructor looks like this
Matrix::~Matrix(){
delete[] tab;
m = n = 0;
}
The latter seems to be easier in implementation, there is no need to call destructor for every Vector in tab. Am I missing some kind of memory leak? If not why would i ever use the first implementation.
std::arrayis exactly that: This container is an aggregate type with the same semantics as a struct holding a C-style array T[N] as its only non-static data member. Except it removed the bothersome decaying to pointer and adds bounds checking in theatmethod, iterators and a bunch of other useful stuff. Get rid of the pointers completely.