问题
I am writing a code which uses matrices and advanced operations on them a lot. I am not going to implement a matrix class from the beginning instead I want to have some sort of Matrix abstract class or interface which I define the methods I want for it but it uses another implemented Matrix class through it's core something like:
template <typename T>
class IMatrix
{
public:
/*some methods and also some operator overloads*/
virtual T operator() (int r, int c)=0;
};
as the interface for my matrix class and
template <typename T>
class ArdalanMatrix :public IMatrix<T>
{
public:
ArdalanMatrix(int r,int c, T val=0.){
numberOfRows = r;
numberOfColumns = c;
innerMatrix.resize(r, c, val);
};
T operator() (int r, int c) {
/*implementation*/
}
private:
techsoft::matrix<T> innerMatrix;//the inside matrix class
int numberOfRows;
int numberOfColumns;
};
I gave you masters an example of what I want to do but I don't know if it is a good approach or a bad one. what do you recommend to me to do these kind of stuff easily?
int main() {
IMatrix<double> *M1 = new ArdalanMatrix<double>(5, 5, 0.);
IMatrix<double> *M2 = new ArdalanMatrix<double>(5, 5, 0.);
IMatrix<double> *M3;
M3 = M1 + M2;//overloaded operator "+"
M3 = M1 * M2;//overloaded operator "*"
}
来源:https://stackoverflow.com/questions/29554383/an-efficient-matrix-interface-in-c