I have a self-defined Matrix
class and want to overload operator *
to do matrix multiplication:
template< int R, int C>
class Matrix{
int *_mat;
int _size;
public:
Matrix(){ _size = R*C; _mat = new int[_size]{0}; }
~Matrix(){ delete []_mat; }
Matrix &operator=(const Matrix & m){/*...*/}
//...
template< int D2, int D1 > using matrix_t = int[D2][D1];
template<int R2, int C2>
Matrix<R,C2> operator*(const matrix_t<R2,C2> &mat)
{
Matrix<R,C2> result;
for(int r = 0; r < R; r++)
{
for(int c = 0; c < C2; c++)
{
for( int i; i < C; i++ ){
/*do multiplication...
result._mat[r*C2+c] = ...
*/
}
}
}
return result;
}
//...
};
Then the problem comes with Matrix<R,C2> result
. The result
becomes a outside object of the class. So I cannot access its private member using like result._mat[r*C2+c]
.
What is the solution( without changing access permission) to define my function of matrix multiplication in this class?