iterators

T

Talha

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!!
 
V

Victor Bazarov

Talha said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top