Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > iterators

Reply
Thread Tools

iterators

 
 
Talha
Guest
Posts: n/a
 
      09-04-2003
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!!
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-04-2003
"Talha" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
plain iterators and reverse iterators on vector subramanian100in@yahoo.com, India C++ 10 08-08-2009 08:28 AM
Iterators and reverse iterators Marcin Kaliciński C++ 1 05-08-2005 09:58 AM
Idempotent ODBMS iterators Paul Chapman Java 0 02-16-2005 01:51 PM
Nested iterators (well, not nested exactly...) Russ Perry Jr Java 2 08-20-2004 06:51 PM
Any interest in lightweight coroutines in Java ala C# 2.0 iterators? Ken Sprague Java 4 10-28-2003 08:03 PM



Advertisments