+ 11
How to make this Matrix Class Work?
Suppose I have a Matrix Class defined with a generic template... So, template<class Mat> class Matrix{ private: Mat **arr; int row, int col; //Memory allocation and deallocation is to be assumed in constructors and destructors... public: Mat Element(int r,int c) {return arr[r][c];} }; Now, I want to use a custom created complex class for the matrix elements... So, will I have to redeclare the function like this? Complex Element(int r,int c); And make a Complex** arr in the private section? Help me...
4 Respuestas
+ 3
I'm not sure exactly what you're going for, but if you want a Matrix of Complex objects, instantiate your matrix like so:
Matrix<Complex> m;
If you want to specialize member functions for Complex objects, then add something like this after your Matrix class:
template<>
void Matrix<Complex>::foo() {
// do something
}
In terms of templates, "overloading" for a specific type is referred to as specialization. You don't need to do this unless your function needs to make exceptional considerations for a specific type. As your class is set up right now, Element should work how you're expecting it to.
+ 12
@Squidy
Thus, this must be done outside the class...
Is there no way to do it inside?
BTW Thank You!
+ 12
@Squidy
So on making an object like this:
Matrix<Complex> a(2,2),b(2,2);
Will they multiply according to the rules in the complex class?
If yes, Atleast specialization is not required here...
+ 11
What I want to know is how to define the overloads for the operations on the complex class elements...