operator * of iterator

This is a discussion on operator * of iterator within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Operator * on iterator of type T will result in reference to type T (T&), right (i.e. not ...

  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
    22,265
    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&
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    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, 09:32 AM
  2. Iterator
    By MarkZWEERS in forum C++ Programming
    Replies: 19
    Last Post: 05-20-2008, 12:16 AM
  3. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 11:01 PM
  4. Link list library
    By Brighteyes in forum C Programming
    Replies: 4
    Last Post: 05-12-2003, 09:49 PM
  5. Search and Build Tree
    By 1999grandamse in forum C++ Programming
    Replies: 17
    Last Post: 11-14-2002, 01:36 PM

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