+ 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...

15th Apr 2017, 4:22 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
4 odpowiedzi
+ 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.
15th Apr 2017, 4:40 AM
Squidy
Squidy - avatar
+ 12
@Squidy Thus, this must be done outside the class... Is there no way to do it inside? BTW Thank You!
15th Apr 2017, 12:00 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 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...
15th Apr 2017, 12:10 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 11
What I want to know is how to define the overloads for the operations on the complex class elements...
15th Apr 2017, 4:23 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar