Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Operator overloading []

Operator overloading []

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 786 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • beeckscheB Offline
    beeckscheB Offline
    beecksche
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • beeckscheB Offline
      beeckscheB Offline
      beecksche
      wrote on last edited by
      #2

      Sorry for disturbance, found my mistake. Solution:

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

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved