Thread: operator * of iterator

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    operator * of iterator

    Hello everyone,


    Operator * on iterator of type T will result in reference to type T (T&), right (i.e. not type T itself or some other types)?

    I am looking for some STL implementation code for this (operator * on iterator of type T will result in reference to type T (T&)), but can not find (maybe I search method is not correct). Could anyone post some internal implementation code please?


    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not sure what you mean by your question?

    Are you referring to the fact that the iterotor returns a reference to the object it refers to, rather than a copy? That is what you want, no? Otherwise, you couldn't use an iterator to modify the content that it refers to.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Operator * on iterator of type T will result in reference to type T (T&), right (i.e. not type T itself or some other types)?
    It depends on the kind of iterator. According to the C++ standard:

    Input iterators: result is convertible to T
    Output iterators: result is not used
    Forward, bi-directional and random access iterators: result is T&
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    template <typename T>
    class iterator
    {
      T* data;
    public:
      T& operator*() { return *data; }
    };
    Is that close enough? That's not a real implementation, but it's really close to what it might look like.

    Another implementation could be this:
    Code:
    typedef T* iterator;
    In which case the operator* is part of the language so you can't actually see the code that implements it.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by George2 View Post
    Operator * on iterator of type T will result in reference to type T (T&), right (i.e. not type T itself or some other types)?
    Non-const iterators usually return a reference, so that the value can be modified. A const iterator could in theory return a value, not a reference, but in practice usually returns a reference for efficiency.

    I am looking for some STL implementation code for this (operator * on iterator of type T will result in reference to type T (T&)), but can not find (maybe I search method is not correct). Could anyone post some internal implementation code please?
    Look in your compiler's STL header files, it's all there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Iterator
    By MarkZWEERS in forum C++ Programming
    Replies: 19
    Last Post: 05-19-2008, 11:16 PM
  3. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  4. Link list library
    By Brighteyes in forum C Programming
    Replies: 4
    Last Post: 05-12-2003, 08:49 PM
  5. Search and Build Tree
    By 1999grandamse in forum C++ Programming
    Replies: 17
    Last Post: 11-14-2002, 01:36 PM