By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

iterators

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
"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.