0

Below the link where I see how iterator begin function works but not understand clearly.

https://secweb.cs.odu.edu/~zeil/cs361/web/website/Lectures/iterators/pages/implem.html

In their, I see begin function but don't understand how it works

  Book::iterator Book::begin()
  {
   Book::iterator it;
   it->pos = authors;
    return it;
  };

in this it is object but in this it-> what is doing i think this overloaded with operator->() I think it work like a it.operator->().pos but I don't know for sure after that why it return it my question is what is return it does I'm not getting clear picture for this.

Xatyrian
  • 1,364
  • 8
  • 26
  • Does this code compile? I can't find the class specification for `Book::iterator`, so I can't tell what the member access operator for `Book::iterator` does. Without the code for `Book::iterator::operator->()`, there's no way to know. – David Schwartz Jan 12 '20 at 07:06
  • @DavidSchwartz https://secweb.cs.odu.edu/~zeil/cs361/web/website/Lectures/iterators/pages/ar01s02s03.html – Abhi Sarkar Jan 12 '20 at 07:10
  • in this book class declare – Abhi Sarkar Jan 12 '20 at 07:10
  • The `->` is the class member reference operator initializing `it->pos` to the first node in `AuthorListNode* authors;` (so your `begin()` iterator returns a pointer with `pos` that points to the first node in the list.) – David C. Rankin Jan 12 '20 at 07:18
  • @AbhiSarkar Maybe I'm blind, but I still can't see where `Book::iterator::operator->()` is defined. – David Schwartz Jan 12 '20 at 08:35
  • **DavidSchwartz** i think you are right actually im getting confuse. – Abhi Sarkar Jan 12 '20 at 09:58

2 Answers2

1

Your class Book contains a struct AuthorListNode and a pointer to type struct AuthorListNode that is used as a pointer to the beginning node in a linked list:

class Book {
  struct AuthorListNode {
     Author data;
     AuthorListNode* next;
  };
  ...
private:
  ...
  AuthorListNode* authors;  // linked list of pointers to authors
  ...
};

In the iterator for Book::iterator Book::begin(), it->pos = authors; sets the pos member of the iterator it to the beginning of your linked-list returning the pointer it with the pos member initialized to point to the start of the linked-list allowing you to iterate from the beginning of authors.

(note: AuthorIterator::pointer AuthorIterator::operator->() has return &(pos->data); so as identified by &AlanBirtles in his answer using it->pos may well be a bug in the documentation)

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • **David C.Rankin** can you refer some sites or tutorial from where i can learn iterator full tutorial – Abhi Sarkar Jan 12 '20 at 09:59
  • I was actually reading through your class syllabus and it does a pretty good job of describing the iterator integration. I would use your guide and the StackOverflow page [How to implement an STL-style iterator and avoid common pitfalls?](https://stackoverflow.com/questions/8054273/how-to-implement-an-stl-style-iterator-and-avoid-common-pitfalls) to get a simple iterator working and you can apply what you learn to the rest. – David C. Rankin Jan 12 '20 at 10:09
  • **David C.Rankin** actually this resource i get from the internet from where i learn this. – Abhi Sarkar Jan 12 '20 at 11:03
  • @AbhiSarkar the `->` is an error in your implementation. I wrote a short iterator implementation from your documentation and you do need `it.pos` instead of `it->pos`, see [Class Iterator](https://susepaste.org/48128894). It was unclear given multiple typedefs over multiple links you cite, but `Book::iterator it;` really does just declare an instance of `Book::iterator` not a pointer to one making `int.pos` proper. – David C. Rankin Jan 12 '20 at 16:43
1

I'm pretty sure this is a bug, it should just be it.pos = authors as it is trying to set the value of the iterator not dereference the iterator (which is not initialised so will probably crash) and set a value on the pointed to node.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60