Results 1 to 6 of 6

Thread: operator [] overloading

  1. #1
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Question operator [] overloading

    Hello qt & c++ fans,

    I have some probs overloading the [] operator of my templated grid class.
    The template class looks basically like this:

    Qt Code:
    1. template<class T> class Grid
    2. {
    3. public:
    4. Grid(int width, int height) : _pMat(NULL), _iWidth(0), _iHeight(0){
    5. _iWidth = width;
    6. _iHeight = height;
    7. _pMat = createGrid(width, height);
    8. }
    9.  
    10. ~Grid(void){
    11. destroyGrid();
    12. }
    13.  
    14. Matrix2Dim(const Matrix2Dim<T> &mat){//cpy...}
    15. Matrix2Dim<T>& operator=(const Matrix2Dim<T> &mat){//cpy...}
    16.  
    17. private:
    18. T** createGrid(int width, int height){
    19. T **pMat = NULL;
    20. pMat = new T *[height];
    21. pMat[0] = new T [height * width];
    22. for (int i = 0; i < height; i++)
    23. pMat[i] = &pMat[0][(i*width)];
    24. return pMat;
    25. }
    26.  
    27. void destroyGrid(void){
    28. if(_pMat != NULL){
    29. delete _pMat[0];
    30. delete[] _pMat;
    31. _pMat = NULL;
    32. _iWidth = _iHeight = 0;
    33. }
    34. }
    35. int _iWidth;
    36. int _iHeight;
    37. T** _pMat;
    38. };
    To copy to clipboard, switch view to plain text mode 

    now I'd like to access data of the class with the [] operator:
    Qt Code:
    1. Grid<int> *test = new Grid<int>(2, 2);
    2. test[1][1] = 1;
    3. std::cout << test[1][1] << std::endl;
    4. delete test;
    To copy to clipboard, switch view to plain text mode 

    everything I tried to overload the [] operator failed :/, so maybe somebody here knows how to do it???
    Qt Code:
    1. T *operator[](int value) {
    2. return ???;
    3. }
    4.  
    5. const T *operator[](int value) const{
    6. return ???;
    7. }
    To copy to clipboard, switch view to plain text mode 

    thanks and regards
    darksaga

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: operator [] overloading

    How about:
    Qt Code:
    1. T *operator[](int value){
    2. return _pMat[value];
    3. }
    To copy to clipboard, switch view to plain text mode 

    Just be aware you won't be able to use it as L-value (I think).

  3. #3
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: operator [] overloading

    I think you should check out what the C++ FAQ Lite has to say on the subject. Especially point 13.11


    P.S.
    That's not to say you shouldn't do it the way you're doing it, but I think you should at least allocate all the memory at once, and then reference it using pointer arithmetic.

  4. #4
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default Re: operator [] overloading

    Quote Originally Posted by wysota View Post
    How about:
    Qt Code:
    1. T *operator[](int value){
    2. return _pMat[value];
    3. }
    To copy to clipboard, switch view to plain text mode 

    Just be aware you won't be able to use it as L-value (I think).
    if I have a 2x2 int grid ==>
    Qt Code:
    1. ----
    2. |1 2|
    3. |3 4|
    4. ----
    To copy to clipboard, switch view to plain text mode 
    and use the code above:

    Qt Code:
    1. Grid<int> *test = new Grid<int>(2, 2);
    2. test->fill();//fill grid that it matches example above
    3. std::cout << test[0][0] << std::endl; // returns an address not a value
    4. std::cout << test[0][1] << std::endl; // returns an address not a value
    5. std::cout << test[1][0] << std::endl; // crash
    6. std::cout << test[1][1] << std::endl; // crash
    7. delete test;
    To copy to clipboard, switch view to plain text mode 

    but if you look at how I allocate my grid in createGrid() its now wonder that it behaves like that.

    i also tried the () operator:
    Qt Code:
    1. T& operator()(int height, int width)
    2. {
    3. return _pMat[height][width];
    4. }
    5.  
    6. const T& operator()(int height, int width)
    7. {
    8. return _pMat[height][width];
    9. }
    To copy to clipboard, switch view to plain text mode 
    but then my compiler won't compile the code...

  5. #5
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: operator [] overloading

    Qt Code:
    1. template<class T>
    2. class Matrix
    3. {
    4. public:
    5. Matrix(int width, int height)
    6. : data(NULL)
    7. , _iWidth(width)
    8. , _iHeight(height)
    9. {
    10. data = new T[width * height];
    11. }
    12. ~Matrix()
    13. {
    14. delete [] data;
    15. }
    16. T* operator[](int row) // index starts with zero
    17. {
    18. return data + row*_iWidth;
    19. }
    20. T const* operator[](int row)const
    21. {
    22. return data + row*_iWidth;
    23. }
    24. T& operator()(int row, int col) // indexs start with one
    25. {
    26. return *(data + (row-1)*_iWidth + col-1);
    27. }
    28. const T& operator()(int row, int col)const
    29. {
    30. return *(data + (row-1)*_iWidth + col-1);
    31. }
    32. private:
    33. int _iWidth;
    34. int _iHeight;
    35. T* data;
    36. };
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to spud for this useful post:

    darksaga (8th April 2008)

  7. #6
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Default Re: operator [] overloading

    thanks @ spud

    especially for this link.

    I had some time to read through the article, now everything works as desired ...

Similar Threads

  1. Qt event queue overloading?
    By gct in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2008, 18:39
  2. Simple: Operator overloading with heap objects
    By durbrak in forum General Programming
    Replies: 12
    Last Post: 25th April 2007, 13:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.