Results 1 to 6 of 6

Thread: Defining a two dimensional QVector class (Q2DDoubleVector)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Defining a two dimensional QVector class (Q2DDoubleVector)

    If I do it in a very similar way, it doesn't crash:
    There is one "small" difference:
    Qt Code:
    1. (1) this[r].resize(columns);
    2. vs.
    3. (2) matrix[r].resize(COLS);
    To copy to clipboard, switch view to plain text mode 
    Line (1) does not call "operator []" on this pointer like (2). Its equivalent to
    Qt Code:
    1. *(this+r).resize(columns);
    To copy to clipboard, switch view to plain text mode 
    Can you see the problem ? Its like you want to access an array of objects using index r, where this should point to the first element. Obviously this array does not exists, so your program crashes.
    You can fix this by changing (1) to:
    Qt Code:
    1. (*this)[r].resize(columns);
    To copy to clipboard, switch view to plain text mode 
    IMHO you should not subclass a QVector, it does not have a virtual destructor. Better use composition instead.

    ----
    edit:
    I've made a simple example to show this pointer arithmetic "magic":
    Qt Code:
    1. #include <QDebug>
    2.  
    3. class Test{
    4. public:
    5. Test(){
    6.  
    7. }
    8. void check(){
    9. qDebug() << "this is" << x << ", next is:" << this[1].x;
    10. }
    11. int x;
    12. };
    13.  
    14. int main(){
    15. Test x[5];
    16. for( int i=0 ; i<5 ; ++i ){
    17. x[i].x = i;
    18. }
    19. for( int i=0 ; i<4 ; ++i ){
    20. x[i].check();
    21. }
    22. }
    23. // outputs:
    24. // this is 0 , next is: 1
    25. // this is 1 , next is: 2
    26. // this is 2 , next is: 3
    27. // this is 3 , next is: 4
    To copy to clipboard, switch view to plain text mode 
    We create an array of 5 objects, assigned unique, ordered values. In method "check" we can access next element in that array, using pointer arithmetic ( this[1] = *(this+1) = next object in array ).
    Don't use it in "real" code, its just for fun
    Last edited by stampede; 25th September 2011 at 22:56.

Similar Threads

  1. Replies: 5
    Last Post: 2nd September 2011, 23:11
  2. Replies: 1
    Last Post: 5th February 2011, 03:04
  3. use QVector as 2 dimensional Array
    By umulingu in forum Qt Programming
    Replies: 3
    Last Post: 1st January 2010, 12:31
  4. How to implement one-dimensional zoom
    By d_stranz in forum Qwt
    Replies: 0
    Last Post: 7th November 2008, 17:46
  5. Multi Dimensional QList
    By aamer4yu in forum Qt Programming
    Replies: 5
    Last Post: 2nd April 2007, 06:46

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.