iterators

Discussion in 'C++' started by Talha, Sep 4, 2003.

  1. Talha

    Talha Guest

    Hi,

    I have a general question about iterators

    suppose I have a class

    class MyClass
    {
    public:
    ....
    private:
    PtrList m_ptrlist<somerecordtype>;
    }

    PtrList<T> is a class taht has an iterator implemented .. so i could
    do:
    m_ptrlist::iterator it = m_ptrlist.begin() .. and iterate / modify
    items etc.

    but suppose i want MyClass to be able to iterate over this list and
    present that iteration interface to the user . what is the correct
    way to do this?

    Thanks!!
    Talha, Sep 4, 2003
    #1
    1. Advertising

  2. "Talha" <> wrote...
    > Hi,
    >
    > I have a general question about iterators
    >
    > suppose I have a class
    >
    > class MyClass
    > {
    > public:
    > ....
    > private:
    > PtrList m_ptrlist<somerecordtype>;


    Huh? Didn't you mean

    PtrList<somerecordtype> m_ptrlist;

    ???

    > }

    ;
    >
    > PtrList<T> is a class taht has an iterator implemented .. so i could
    > do:
    > m_ptrlist::iterator it = m_ptrlist.begin() .. and iterate / modify
    > items etc.


    No, you couldn't. 'm_ptrlist' is not a type nor is it a namespace.
    You have to do

    PtrList<somerecordtype>::iterator it = ...

    > but suppose i want MyClass to be able to iterate over this list and
    > present that iteration interface to the user . what is the correct
    > way to do this?


    There probably more than one correct way. You could define your own
    type (MyClass::iterator, for example) by typedef'ing the PtrList's
    one:

    class MyClass {
    ...
    typedef PtrList<somerecordtype>::iterator iterator;
    };

    and then provide your own 'begin', and 'end' member functions that
    would return the right iterators:

    class MyClass {
    ...
    iterator begin() { return m_ptrlist.begin(); }
    iterator end() { return m_ptrlist.end(); }
    };

    That ought to do it...

    Victor
    Victor Bazarov, Sep 4, 2003
    #2
    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. Ken Sprague
    Replies:
    4
    Views:
    683
  2. Russ Perry Jr
    Replies:
    2
    Views:
    4,124
    Russ Perry Jr
    Aug 20, 2004
  3. Paul Chapman

    Idempotent ODBMS iterators

    Paul Chapman, Feb 16, 2005, in forum: Java
    Replies:
    0
    Views:
    430
    Paul Chapman
    Feb 16, 2005
  4. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    485
    Kai-Uwe Bux
    May 8, 2005
  5. , India
    Replies:
    10
    Views:
    1,075
    James Kanze
    Aug 8, 2009
Loading...

Share This Page