I have seen in numerous texts that the term “cursor” is used interchangeably with the term “iterator”. However, it seems wrong that they are treated as one and the same.
For me, an iterator allows iteration of a container without knowing the container itself. On the other hand, the cursor also allows iteration of the container, but has implementation details specific to the type of container, so it retains a reference to the container. In addition, the cursor interface reflects a container type interface similar to a facade texture.
Here is an example of what I consider the cursor:
class Book {}; class Library { std::vector<Book> books; bool IsBookHardCover( int bookIndex ); bool IsBookSoftCover( int bookIndex ); BookCursor GetFirstBook(); }; class BookCursor { std::vector<Book>& books; int currentBook; bool IsHardCover(); bool IsSoftCover(); void Next(); };
So basically I am making a distinction between iterators and cursors based on their dependency or knowledge of the container they use. Is this a suitable difference? If not, what would you think of the design pattern that I described in the above code example?
Please note that my code sample above should be considered as pseudo-code, since I did not compile it, and it also does not need the required constructors.
source share