template <typename T>
class Table {
public:
Table();
Table(int m, int n);
Table(int m, int n, const T& value);
Table(const Table<T>& rhs);
~Table();
Table<T>& operator=(const Table& rhs);
T& operator()(int i, int j);
int numRows()const;
int numCols()const;
void resize(int m, int n);
void resize(int m, int n, const T& value);
private:
void destroy();
private:
int mNumRows;
int mNumCols;
T** mDataMatrix;
};
template <typename T>
void Table<T>::destroy() {
if (mDataMatrix) {
for (int i = 0; i < _m; ++i) {
if (mDataMatrix[i]) {
delete[]mDataMatrix[i];
mDataMatrix[i] = 0;
}
}
delete[] mDataMatrix;
mDataMatrix = 0;
}
mNumRows = 0;
mNumCols = 0;
}
This is a sample code that I got from a book. It demonstrates how to destroy or free a 2x2 matrix, where mDataMatrix is a pointer to an array of pointers.
I do not understand this part:
for(int i = 0; i < _m; ++i) {
if (mDataMatrix[i]) {
}
}
I do not know why the book uses _m for the maximum number of row-ptr. There was not even a variable in the class; the variable for max row is mNumRows. Maybe this is a specific compiler variable? Another thing is that I'm rather confused, why is it ++ i? pre-operator, why not i ++? Will it be different if I change it to i ++?
source
share