Iterator assignment from a pointer

Discussion in 'C++' started by Dom Gilligan, Aug 18, 2005.

  1. Dom Gilligan

    Dom Gilligan Guest

    What exactly can you assign to a set iterator, by assignment or
    initialisation? (is this covered in Josuttis? I can't find it).

    Some (limited) digging around in the Gnu code shows no operator=, but
    2 ctors for iterators, and 3 for const_iterators. For const_iterator,
    there's a no-parameter ctor, and a ctor which takes another
    const_iterator. The 3rd one is more interesting - it takes a pointer
    to a node in the R/B tree, and the iterator itself is actually a
    pointer to a node.

    This makes me think that there may actually be a way to assign a
    pointer to an iterator, with the right runes. If there isn't one, can
    anyone tell me why this isn't allowed?

    Thanks

    Dom
     
    Dom Gilligan, Aug 18, 2005
    #1
    1. Advertisements

  2. Dom Gilligan wrote:
    > What exactly can you assign to a set iterator, by assignment or
    > initialisation? (is this covered in Josuttis? I can't find it).


    You mean like

    set<int>::iterator it;

    it = ??? // what is allowed here?

    From what I see in the Standard, the iterators for containers are
    implementation-defined, and the only requirement on the std::set iterators
    is that they are of the bidirectional kind. The only assignment defined
    for a bidirectional iterator is from another bidirectional iterator.

    > Some (limited) digging around in the Gnu code shows no operator=, but
    > 2 ctors for iterators, and 3 for const_iterators. For const_iterator,
    > there's a no-parameter ctor, and a ctor which takes another
    > const_iterator. The 3rd one is more interesting - it takes a pointer
    > to a node in the R/B tree, and the iterator itself is actually a
    > pointer to a node.


    The absence of an explicit operator= suggests that only the compiler-
    generated copy assignment can be used. Just like I said, only assignment
    from another iterator.

    > This makes me think that there may actually be a way to assign a
    > pointer to an iterator, with the right runes. If there isn't one, can
    > anyone tell me why this isn't allowed?


    Assignment from a pointer will yield creation of a temporary iterator
    from the pointer and the assignment from that iterator:

    set<int>::iterator it;
    it = somepointer;

    is the same as

    set<int>::iterator it;
    it = set<int>::iterator(somepointer);

    Since set<int>::iterator is implementation-defined, there is nothing
    in C++ to say about what constructors it should or should not have.

    V
     
    Victor Bazarov, Aug 18, 2005
    #2
    1. Advertisements

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. Hendrik Maryns
    Replies:
    18
    Views:
    1,748
  2. Replies:
    10
    Views:
    988
    Chris Torek
    Feb 4, 2005
  3. jimjim
    Replies:
    16
    Views:
    1,108
    Jordan Abel
    Mar 28, 2006
  4. David Bilsby
    Replies:
    5
    Views:
    2,401
    David Bilsby
    Oct 9, 2007
  5. Replies:
    4
    Views:
    1,646
    Fred Zwarts
    Jul 2, 2009
  6. A
    Replies:
    7
    Views:
    804
  7. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    781
    James Kuyper
    Sep 23, 2011
  8. arnuld

    pointer vs pointer to pointer

    arnuld, Jun 11, 2012, in forum: C Programming
    Replies:
    4
    Views:
    643
    Ben Bacarisse
    Jun 11, 2012
Loading...