I actually have 2 questions about the same idea:
I want to create a TruthMatrix class, How can i:
- allocate a dynamic nXn matrix of bools, is the only way of doing it is as following?:
class TruthMatrix {
bool **mat;
public:
TruthMatrix(int n) {
mat=new bool*[n];
for (int i=0;i<n;i++) {
mat[i]=new bool[n];
}
}
};
- override the [][] operator for quick access to the matrix elements such in mat[i][j]
Thanks!
No, it’s not the only way. You could simulate a matrix with one large array, and you could use STL containers to ease memory management (for one). (Or use
dynamic_bitsetor similar).It’s probably not worth it. It’s common for matrix classes to be subscripted with
operator()because it’s much easier to implement. To do it with several square brackets (akin to arrays of arrays) you need a suitable proxy object to return from youroperator[](do note that there’s no such thing asoperator[][]).Example: