C
check.checkta
Hi,
I'd like to implement a simple matrix class. I'd like to overload
operator [] so that it returns as a vector (either the stl vector or
some other Vector class of my own). The reason I want to do this is
because it enables me to apply some functions of a vector to a row of
of matrix, e.g., I have a function to compute the sum of vector:
double my_sum(vector<double> x)
{
int i, n=x.size();
double result = 0;
for(i=0; i<n; ++i) result += x;
return result;
}
I'd like to compute the sum of the second row of a matrix:
Matrix<double> a(3,5);
double sum2 = my_sum(a[1]);
A simple one would have three private members, the number of rows,
columns and a double pointer. If operator[] returns a pointer to row
i, then I can't call my_sum(a[1]). If I force operator[] to return a
vector<T> it won't compile. Please help.
Thanks a lot.
Below is a snippet of the template class:
=======================================
template <class T>
class Matrix {
private:
int r;
int c;
T **dp;
public:
inline T* operator[](const int i);
inline const T* operator[](const int i) const;
};
template <class T>
inline T* Matrix<T>:perator[](const int i)
{
return dp;
}
template <class T>
inline const T* Matrix<T>:perator[](const int i) const
{
return dp;
}
I'd like to implement a simple matrix class. I'd like to overload
operator [] so that it returns as a vector (either the stl vector or
some other Vector class of my own). The reason I want to do this is
because it enables me to apply some functions of a vector to a row of
of matrix, e.g., I have a function to compute the sum of vector:
double my_sum(vector<double> x)
{
int i, n=x.size();
double result = 0;
for(i=0; i<n; ++i) result += x;
return result;
}
I'd like to compute the sum of the second row of a matrix:
Matrix<double> a(3,5);
double sum2 = my_sum(a[1]);
A simple one would have three private members, the number of rows,
columns and a double pointer. If operator[] returns a pointer to row
i, then I can't call my_sum(a[1]). If I force operator[] to return a
vector<T> it won't compile. Please help.
Thanks a lot.
Below is a snippet of the template class:
=======================================
template <class T>
class Matrix {
private:
int r;
int c;
T **dp;
public:
inline T* operator[](const int i);
inline const T* operator[](const int i) const;
};
template <class T>
inline T* Matrix<T>:perator[](const int i)
{
return dp;
}
template <class T>
inline const T* Matrix<T>:perator[](const int i) const
{
return dp;
}