iterators and class members

This is a discussion on iterators and class members within the C++ Programming forums, part of the General Programming Boards category; I read somewhere that when using iterators to refer to struct/class members that the arrow operator -> should not be ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595

    iterators and class members

    I read somewhere that when using iterators to refer to struct/class members that the arrow operator -> should not be used and that the syntax should be (*it).member, or whatever. Is that correct? I read some about STL and such, but don't have compliant compiler so can't verify myself. Thanks.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Doing a brief scan of the standard, I can't find any mention of operator -> for iterators. However, code using iterator::operator-> compiles fine on Comeau C++ in strict mode, which is one of the most conforming compilers. Josuttis uses -> in "The C++ Standard Library", and Stroustrup uses it in "The C++ Programming Language", so I'd guess it is a safe practice.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    pretty reputable sources, too, I'd say. I looked up my reference and it says that many iterator definitions do not overload the pointer operator...with these iterators field access must be performed using the combination of * and filed access dot, and not using the pointer arrow. (Budd---Data Structures in C++ using the STL. Therefore, it would seem that iterators don't come prequipped with the arrow operator like pointers do, so if you have problems using the pointer operator use the (*). syntax.

    Thanks for your assistance Sorenson.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dereferencing vector iterators
    By mmfuller in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2008, 01:48 PM
  2. Adding iterators to a class template
    By skewray in forum C++ Programming
    Replies: 8
    Last Post: 01-03-2008, 10:33 PM
  3. Class templates and iterators
    By creativeinspira in forum C++ Programming
    Replies: 2
    Last Post: 06-30-2007, 03:35 PM
  4. Declare a template class as a friend?
    By AH_Tze in forum C++ Programming
    Replies: 11
    Last Post: 05-19-2004, 09:24 PM
  5. Partial specialization of class template.
    By CornedBee in forum C++ Programming
    Replies: 4
    Last Post: 11-20-2003, 08:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21