Syntactical issue: should STL list Iterator be overloaded to a pointer to the current element?

Discussion in 'C++' started by Olumide, Sep 11, 2007.

  1. Olumide

    Olumide Guest

    Hello,

    To begin with, I'm no STL expert. I've only just begun to use and
    appreciate the power of STL.

    Okay, so I have a list:

    list<int> myLuckyNumbers;
    list<int>::iterator intList_Iterator = myLuckyNumbers->begin();

    while( intList_Iterator != myLuckyNumbers->end() )
    {
    int *integerPointer = &(*intList_Iterator);

    intList_Iterator++;
    }


    In order to get a pointer to each element of the list, I need to use
    the unintuitive expression, &(*intList_Iterator). My worry is that
    because the dereference (*) and "address of" (&) operators, cancel
    each other, shouldn't intList_Iterator be interpreted as a pointer to
    a int, if assigned to an int?

    Also, is there a better way getting the pointer to the current in in
    the list?

    Thanks,

    - Olumide
     
    Olumide, Sep 11, 2007
    #1
    1. Advertising

  2. Olumide wrote:
    > To begin with, I'm no STL expert. I've only just begun to use and
    > appreciate the power of STL.
    >
    > Okay, so I have a list:
    >
    > list<int> myLuckyNumbers;
    > list<int>::iterator intList_Iterator = myLuckyNumbers->begin();
    >
    > while( intList_Iterator != myLuckyNumbers->end() )
    > {
    > int *integerPointer = &(*intList_Iterator);
    >
    > intList_Iterator++;
    > }
    >
    >
    > In order to get a pointer to each element of the list, I need to use
    > the unintuitive expression, &(*intList_Iterator). My worry is that
    > because the dereference (*) and "address of" (&) operators, cancel
    > each other,


    It's a misconception. The dereference operator does not cancel the
    address-of operator. They can (and the dereference is) overloaded
    for the iterator object. No canceling occurs.

    > shouldn't intList_Iterator be interpreted as a pointer to
    > a int, if assigned to an int?
    >
    > Also, is there a better way getting the pointer to the current in in
    > the list?


    Better? In what way "better"?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 11, 2007
    #2
    1. Advertising

  3. Olumide

    Olumide Guest

    On 11 Sep, 21:11, "Victor Bazarov" <> wrote:
    > Better? In what way "better"?


    Better as in other than &(*intList_Iterator) .
     
    Olumide, Sep 11, 2007
    #3
  4. Olumide

    Jim Langston Guest

    "Olumide" <> wrote in message
    news:...
    > On 11 Sep, 21:11, "Victor Bazarov" <> wrote:
    >> Better? In what way "better"?

    >
    > Better as in other than &(*intList_Iterator) .


    &(*iterator)
    is the common method used to get the address of what iterator points to.
    After you see it a few times it no longer looks weird.
     
    Jim Langston, Sep 11, 2007
    #4
  5. Olumide

    Pete Becker Guest

    On 2007-09-11 16:36:01 -0400, "Jim Langston" <> said:

    > "Olumide" <> wrote in message
    > news:...
    >> On 11 Sep, 21:11, "Victor Bazarov" <> wrote:
    >>> Better? In what way "better"?

    >>
    >> Better as in other than &(*intList_Iterator) .

    >
    > &(*iterator)
    > is the common method used to get the address of what iterator points to.
    > After you see it a few times it no longer looks weird.


    It looks weird that way. The parentheses aren't needed. <g>

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)
     
    Pete Becker, Sep 11, 2007
    #5
  6. Olumide

    Olumide Guest

    On 11 Sep, 21:49, Pete Becker <> wrote:
    > On 2007-09-11 16:36:01 -0400, "Jim Langston" <> said:
    >
    > > "Olumide" <> wrote in message
    > >news:...
    > >> On 11 Sep, 21:11, "Victor Bazarov" <> wrote:
    > >>> Better? In what way "better"?

    >
    > >> Better as in other than &(*intList_Iterator) .

    >
    > > &(*iterator)
    > > is the common method used to get the address of what iterator points to.
    > > After you see it a few times it no longer looks weird.

    >
    > It looks weird that way. The parentheses aren't needed. <g>


    &*iterator looks mucho weird :) .
     
    Olumide, Sep 11, 2007
    #6
  7. Olumide

    Pete Becker Guest

    On 2007-09-11 16:55:14 -0400, Olumide <> said:

    > On 11 Sep, 21:49, Pete Becker <> wrote:
    >> On 2007-09-11 16:36:01 -0400, "Jim Langston" <> said:
    >>
    >>> "Olumide" <> wrote in message
    >>> news:...
    >>>> On 11 Sep, 21:11, "Victor Bazarov" <> wrote:
    >>>>> Better? In what way "better"?

    >>
    >>>> Better as in other than &(*intList_Iterator) .

    >>
    >>> &(*iterator)
    >>> is the common method used to get the address of what iterator points to.
    >>> After you see it a few times it no longer looks weird.

    >>
    >> It looks weird that way. The parentheses aren't needed. <g>

    >
    > &*iterator looks mucho weird :) .


    Not at all, unless you don't know the language well enough to understand it.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)
     
    Pete Becker, Sep 11, 2007
    #7
  8. Olumide

    Bo Persson Guest

    Olumide wrote:
    :: On 11 Sep, 21:49, Pete Becker <> wrote:
    ::: On 2007-09-11 16:36:01 -0400, "Jim Langston"
    ::: <> said:
    :::
    :::: "Olumide" <> wrote in message
    :::: news:...
    ::::: On 11 Sep, 21:11, "Victor Bazarov" <>
    ::::: wrote:
    :::::: Better? In what way "better"?
    :::
    ::::: Better as in other than &(*intList_Iterator) .
    :::
    :::: &(*iterator)
    :::: is the common method used to get the address of what iterator
    :::: points to. After you see it a few times it no longer looks weird.
    :::
    ::: It looks weird that way. The parentheses aren't needed. <g>
    ::
    :: &*iterator looks mucho weird :) .

    Not really, if you think about it:

    *iterator

    gives you access to an object, so

    &*iterator

    is the address of that object.


    You will hardly ever have to do this anyway, as in C++ code there is
    not much use for a pointer, if you already have an interator.

    Probably it is mostly used in interfacing to old C code APIs, which in
    C++ looks mucho weirdo anyway.


    Bo Persson
     
    Bo Persson, Sep 11, 2007
    #8
  9. Olumide

    Olumide Guest

    On 11 Sep, 22:54, "Bo Persson" <> wrote:
    > Olumide wrote:
    > :: &*iterator looks mucho weird :) .
    >
    > Not really, if you think about it:
    >
    > *iterator
    >
    > gives you access to an object, so
    >
    > &*iterator
    >
    > is the address of that object.


    Thanks. I know unary operators are right associative ... Its just that
    dereferencing and then taking the address of a variable *plain* C++ is
    horribly redundant redundant.
     
    Olumide, Sep 11, 2007
    #9
  10. Re: Syntactical issue: should STL list Iterator be overloaded to apointer to the current element?

    On Tue, 2007-09-11 at 13:03 -0700, Olumide wrote:
    > Hello,
    >
    > To begin with, I'm no STL expert. I've only just begun to use and
    > appreciate the power of STL.
    >
    > Okay, so I have a list:
    >
    > list<int> myLuckyNumbers;
    > list<int>::iterator intList_Iterator = myLuckyNumbers->begin();
    >
    > while( intList_Iterator != myLuckyNumbers->end() )
    > {
    > int *integerPointer = &(*intList_Iterator);
    >
    > intList_Iterator++;
    > }
    >
    >
    > In order to get a pointer to each element of the list, I need to use
    > the unintuitive expression, &(*intList_Iterator). My worry is that
    > because the dereference (*) and "address of" (&) operators, cancel
    > each other, shouldn't intList_Iterator be interpreted as a pointer to
    > a int, if assigned to an int?
    >
    > Also, is there a better way getting the pointer to the current in in
    > the list?


    You could try:

    template<typename T>
    T* iter_pointer(T* iter)
    {
    return iter;
    }

    template<typename T>
    typename std::iterator_traits<T>::pointer iter_pointer(const T& iter)
    {
    return iter.operator->();
    }

    ....

    iterator_traits<list<int>::iterator>::pointer integerPointer
    = iter_pointer(intList_Iterator);

    This neatly avoids going through *intList_Iterator so iterators whose
    pointer types are smart and reference count and stuff will work without
    undefined behaviour until C++ finally supports generic smart references.

    Unfortunately I don't know how to cope with the C++0x proposal to allow
    nonmember operator-> :(

    --
    Tristan Wibberley

    Any opinion expressed is mine (or else I'm playing devils advocate for
    the sake of a good argument). My employer had nothing to do with this
    communication.
     
    Tristan Wibberley, Sep 12, 2007
    #10
  11. In message <>,
    Olumide <> writes
    >Hello,
    >
    >To begin with, I'm no STL expert. I've only just begun to use and
    >appreciate the power of STL.
    >
    >Okay, so I have a list:
    >
    > list<int> myLuckyNumbers;
    > list<int>::iterator intList_Iterator = myLuckyNumbers->begin();
    >
    > while( intList_Iterator != myLuckyNumbers->end() )
    > {
    > int *integerPointer = &(*intList_Iterator);
    >
    > intList_Iterator++;
    > }
    >
    >
    >In order to get a pointer to each element of the list,


    Nobody in this thread seems to have asked what to me is the obvious
    question: why do you _want_ a pointer to each element? If all you need
    is something that "points at" the element, the iterator is already a
    model of the concept of pointer, so (as Bo Persson points out) for most
    purposes it's sufficient to use the iterator directly.

    >I need to use
    >the unintuitive expression, &(*intList_Iterator). My worry is that
    >because the dereference (*) and "address of" (&) operators, cancel
    >each other, shouldn't intList_Iterator be interpreted as a pointer to
    >a int, if assigned to an int?
    >
    >Also, is there a better way getting the pointer to the current in in
    >the list?
    >
    >Thanks,


    --
    Richard Herring
     
    Richard Herring, Sep 17, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    652
    Jim Langston
    Oct 30, 2005
  2. Szabolcs Borsanyi
    Replies:
    6
    Views:
    665
    Ben Bacarisse
    May 23, 2008
  3. Rui Maciel
    Replies:
    6
    Views:
    1,206
    Rui Maciel
    May 11, 2010
  4. Pete Elmore
    Replies:
    6
    Views:
    172
    Pete Elmore
    Nov 4, 2004
  5. Mel
    Replies:
    5
    Views:
    122
Loading...

Share This Page