How do I overload () operator with two parameters; like (3,5)?
- by hkBattousai
I have a mathematical matrix class. It contains a member function which 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" is define as "std::vector<T> TheMatrix"
TheMatrix.at(m_unColSize * unRow + unCol) = Element;
}
catch(std::out_of_range & e)
{
// Do error handling here
}
}
I'm using this method in my code like this:
// 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 things, like below:
Matrix<double> matrix(2, 3);
matrix(1, 2) = 6.78; // HOW DO I DO THIS?