470,631 Members | 2,035 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,631 developers. It's quick & easy.

iterators

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!!
Jul 19 '05 #1
1 2571
"Talha" <ta*********@hotmail.com> 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
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Steven Bethard | last post: by
18 posts views Thread by deancoo | last post: by
1 post views Thread by Marcin Kaliciñski | last post: by
3 posts views Thread by codefixer | last post: by
8 posts views Thread by babak | last post: by
24 posts views Thread by Lasse Vågsæther Karlsen | last post: by
14 posts views Thread by Jiri Kripac | last post: by
2 posts views Thread by ma740988 | last post: by
90 posts views Thread by John Salerno | last post: by
18 posts views Thread by desktop | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.