I have a math matrix class. It contains a member function that is used to access any element of the class.
template<class T>
class Matrix
{
public:
void SetElement(T dbElement, uint64_t unRow, uint64_t unCol);
};
template <class T>
void Matrix<T>::SetElement(T Element, uint64_t unRow, uint64_t unCol)
{
try
{
TheMatrix.at(m_unColSize * unRow + unCol) = Element;
}
catch(std::out_of_range & e)
{
}
}
I use this method in my code as follows:
// create a matrix with 2 rows and 3 columns whose elements are double
Matrix<double> matrix(2, 3);
// change the value of the element at 1st row and 2nd column to 6.78
matrix.SetElement(6.78, 1, 2);
This works well, but I want to use operator overloading to simplify, like below:
Matrix<double> matrix(2, 3);
matrix(1, 2) = 6.78;
source
share