Re: Implementing custom iterators
From: Adriano Dal Bosco (adbosco_at_kais.kyoto-u.ac.jp)
Date: 05/05/04
- Next message: John Carson: "Re: template trick?"
- Previous message: Alf P. Steinbach: "Re: Visual C++ 7.1 INTERNAL COMPILER ERROR"
- In reply to: Chris Schadl: "Implementing custom iterators"
- Next in thread: Chris Schadl: "Re: Implementing custom iterators"
- Reply: Chris Schadl: "Re: Implementing custom iterators"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 5 May 2004 09:42:18 +0900
"Chris Schadl" <cschadl@satan.blah.org.uk> wrote in message
news:pan.2004.05.04.20.49.29.904680@satan.blah.org.uk...
> Hi,
>
> I've written a simple sorted linked list class that I'm trying to
> implement an iterator for. I'm trying to make the interface to the
> iterator simmilar to the STL's iterator interface, so one could do:
>
> SortedList<int> sl;
> SortedList<int>::iterator i;
>
> for (i = sl.begin() ; i != sl.end() ; ++) { /* ... */ }
>
> Right now I'm kind of stuck with the following code:
>
> template <typename T>
> class SortedList
> {
> private:
> // To make life easier, our sorted list will be doubly-linked
> struct ListNode {
> ListNode *next;
> ListNode *prev;
>
> T data;
>
> ListNode() { next = prev = NULL; }
> } *head, *tail;
>
> int size;
>
> protected:
> ListNode* _find(const T&) const;
>
> public:
> SortedList(); // Default constructor
> SortedList(const SortedList&); // Copy constructor
> virtual ~SortedList(); // Destructor
>
> // ... Other methods (insert(), remove(), etc...
>
> class _SortedListIterator {
> private:
> ListNode *cur; // Current element in list
> public:
> _SortedListIterator() : cur(NULL) { }
> _SortedListIterator(ListNode *pos) : cur(pos) { }
> T& operator*();
> void operator++(); // Prefix
> void operator++(int);// Postfix
>
> friend bool operator==(_SortedListIterator&,
_SortedListIterator&);
> friend bool operator!=(_SortedListIterator&,
_SortedListIterator&);
> };
>
> typedef _SortedListIterator iterator;
>
> iterator begin() { iterator iter(head); return iter; }
> iterator end() {iterator iter(NULL); return iter; }
> };
>
> Now, of course this dosen't even compile,
try
friend bool operator==(const _SortedListIterator&, const
_SortedListIterator&);
friend bool operator!=(const _SortedListIterator&, const
_SortedListIterator&);
It should compile (it worked to me) and besides, this is what you mean, I
guess. If you are only comparing two objects, you don't mean to change any
of them. I don't understand why you are making these operators friends,
since you are declaring them iside the class SortedList. You might as well
declare them like this:
bool operator==(const _SortedListIterator&) const;
bool operator!=(const _SortedListIterator&) const;
> I'm not entierly certian if I should declare _SotredListIterator as a
template class, or if I did so, how it
> would effect the _SortedListIterator typedef
I am sorry, I don't know the answer for this one. I will leave it to
someone else to answer. But I think that if you are not using a type other
than T in _SotredListIterator there is no reason to declare it as a
template. Unless, of course, you declare _SotredListIterator out of
SortedList, which for some reason (I don't know why) is more common.
> (AFAIK, typedef's cannot be used with template typenames).
You mean something like this ? :
_SortedListIterator<T> iterator;
Why not?
> Also, would I have establish friendship between SortedList and
_SortedListIterator so _SortedListIterator could
> access SortedLists private members?
Again, it depens on wether _SotredListIterator belongs or not to SortedList.
If not, you should need to declare it as a friend so that it can access cur.
I am sorry for not having any strait and clear answer to your questions. I
only gave my opinion. I hope someone else can clarify this for us.
Regards
Adriano
- Next message: John Carson: "Re: template trick?"
- Previous message: Alf P. Steinbach: "Re: Visual C++ 7.1 INTERNAL COMPILER ERROR"
- In reply to: Chris Schadl: "Implementing custom iterators"
- Next in thread: Chris Schadl: "Re: Implementing custom iterators"
- Reply: Chris Schadl: "Re: Implementing custom iterators"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|