How do I overload () operator with two parameters; like (3,5)?

Posted by hkBattousai on Stack Overflow See other posts from Stack Overflow or by hkBattousai
Published on 2010-12-25T12:46:27Z Indexed on 2010/12/25 12:54 UTC
Read the original article Hit count: 228

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about matrix