Results 1 to 4 of 4

Thread: Pointers...

  1. #1
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Pointers...

    Hi.
    Can someone shed a a bit og light on the differences between the following
    Qt Code:
    1. QVector <int*> *rect;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QVector <int> *rect;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QVector <int*> rect;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QVector <int> rect;
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Pointers...

    What don't you understand about pointers?

    They are not that hard to understand, if you think about "storing" the address of a variable (a good analogy is the address of a house, on an envelope you put the pointer (address) not the actual object(house))

    I'll tell you a little about the code you post (but i don't think that will help you understand pointers) so please ask what you don't understand
    Qt Code:
    1. QVector <int*> *rect; /* rect is a pointer to a QVector <witch stores pointers to ints> carefull with uninitialized pointers, here you don't have the actual QVector, only a pointer*/
    2. QVector <int*> rect; // rect is a QVector <in witch you can add pointers to int>
    3. QVector <int> *rect; // rect is a pointer to a QVector <witch stores integers> carefull with uninitialized pointers... same as first case
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Pointers...

    Thanks for the reply...
    In my project i am using QGraphicsView/Scene and Items .Since i have to have many items which i dont know at runtime so i decided to have a vector of it ..
    so after this consideration i wrote something like
    Qt Code:
    1. H_file : QVector <QGraphicsRectItem> rect;
    2. then i tried to setRect the rect like
    3. CPP_file : rect.at(0).setRect(QReal)....
    To copy to clipboard, switch view to plain text mode 
    but this throws a runtime error..
    I am not good at pointers but i searched for QVector in this forum and found something similar to what i had been doing so i tried to implement that code which was
    Qt Code:
    1. H_file : QVector <QGraphicsRectItem *> *rect;
    2. CPP_file : rect = new QVector<QGraphicsRectItem>();
    3. and the rect->at(0)->setRect...
    To copy to clipboard, switch view to plain text mode 
    but again a run time error follow..

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Pointers...

    That is because, this code:
    Qt Code:
    1. QVector <QGraphicsRectItem> rect; //rect has "place" for 0 elements
    To copy to clipboard, switch view to plain text mode 
    create an empty QVector, and you need to use:
    Qt Code:
    1. QVector <QGraphicsRectItem> rect;
    2. rect.push_back(someInstanceOf_QGraphicRectItem); //this add one element (at the end) to the empty QVector
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. QVector <QGraphicsRectItem> rect(10); //10 is the number of elements, it can grow, with push_back or resize...
    2. rect[0] = someInstanceOf_QGraphicRectItem; //or rect[0].set....
    3. ... // this way (operator[]) the position is not checked, so carefull
    To copy to clipboard, switch view to plain text mode 
    rect.at(positon) it's for accessing elements, not for set the elements, so it returns the const element at "position" (with check that the position is valid)

    QVector reference
    Last edited by Zlatomir; 8th June 2010 at 03:05.

  5. The following 2 users say thank you to Zlatomir for this useful post:

    salmanmanekia (8th June 2010), zoz (10th June 2010)

Similar Threads

  1. Pointers: NULL vs. 0
    By hackerNovitiate in forum Newbie
    Replies: 6
    Last Post: 2nd February 2010, 16:20
  2. help with pointers and
    By sincnarf in forum Qt Programming
    Replies: 6
    Last Post: 2nd July 2007, 15:09
  3. pointers in c++
    By :db:sStrong in forum General Programming
    Replies: 1
    Last Post: 4th June 2007, 09:56
  4. Why should I use pointers ?
    By probine in forum Newbie
    Replies: 5
    Last Post: 9th December 2006, 21:16
  5. pointers gone bad
    By illuzioner in forum General Programming
    Replies: 2
    Last Post: 22nd April 2006, 21:40

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.