Operator overloading []



  • Hi,
    in my template matrix class i want to acces the data with the [] - operator. The data of the matrix is stored in two QVectors:

    QVector<QVector<T>> m_data;
    

    I can set the data with

    template <typename T>
    void Matrix<T>::set(const uint &row, const uint &column, const T &value)
    {
        m_data[row][column] = value;
    }
    

    but sometimes it would be nicer to acces the data with the [] - operator, like

    Matrix<int> mat(3, 3); // 3x3 matrix
    mat[1][2] = 4;
    

    I've tried to overload the operator, but with no success.

    Thanks for help.



  • Sorry for disturbance, found my mistake. Solution:

    template <typename T>
    QVector<T>&  Matrix<T>::operator [](uint index)
    {
        return m_data[index];
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.