Results 1 to 7 of 7

Thread: pointer and iterator

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default pointer and iterator

    Hi, could anyone explain me why the two line are ok?
    Qt Code:
    1. std::vector<Side>* _sides;
    2. std::vector<Side>::iterator its;
    3. int temp;
    4. for (its = _sides->begin(); its != _sides->end(); ++its)
    5. temp += (*its).getSide(); //1
    6. temp += its->getSide(); //2
    To copy to clipboard, switch view to plain text mode 
    Could explain me what scenario we have in term of pointers (refering to its. eg: in the loop its pointer to.........)

    Thank you.
    Regards

  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: pointer and iterator

    If "x" is an iterator then "*x" returns a reference on the object pointed by the iterator and "x->" is a shortcut for "(*x).". This all has nothing to do with pointers. "*" is the dereference operator.

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pointer and iterator

    Quote Originally Posted by wysota View Post
    If "x" is an iterator then "*x" returns a reference on the object pointed by the iterator and "x->" is a shortcut for "(*x).". This all has nothing to do with pointers. "*" is the dereference operator.
    I was going to say that it has nothing to do with iterators, but only with pointers because dereferencing is strictly related to pointers.

  4. #4
    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: pointer and iterator

    I'd say you could dereference anything that is a reference to something else. By dereferencing a street address, you get the house object "pointed to" by the address Iterator is such an address (but not a pointer in strict sense).

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: pointer and iterator

    Quote Originally Posted by wysota View Post
    If "x" is an iterator then "*x" returns a reference on the object pointed by the iterator and "x->" is a shortcut for "(*x).". This all has nothing to do with pointers. "*" is the dereference operator.
    Then if the '->' is a shortcut wich do I have to use? '(*x)' or 'x->'? Is there any reason to choice one?
    Regards

  6. #6
    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: pointer and iterator

    "(*x)." is five characters and "x->" is three characters. The choice is yours to make

  7. #7
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: pointer and iterator

    And if x is itself a large expression or chain, you'll have to enclose that in brackets too. It would make the dereference very unreadable.

    a->b->c->D

    or

    (*(a->b->c)).D
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

Similar Threads

  1. QLinkedList iterator
    By ^NyAw^ in forum Qt Programming
    Replies: 8
    Last Post: 18th October 2007, 17:15
  2. vector iterator as pointer problem
    By Teerayoot in forum General Programming
    Replies: 3
    Last Post: 6th May 2007, 21:36

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.