CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Threaded View

  1. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Why do I have to dereference my vector iterator? What does it mean?

    The real reason behind it all is because it made sense to the people that designed the STL classes.

    What you need to realise is that the iterators are NOT pointers to container elements. Neither are they references or indices or actual items. they are what they are... iterators...

    You may look at one specific implementation ans see that that specific implementation solved the iterator 'problem' by using pointers (most do), but this isn't a guarantee or a requirement of the iterators.

    An iterator is a object that allows you to iterate through a container. Nothing more... nothing less.

    now, you need a way to obtain the vector item the iterator 'points' to.
    The STL designers could have done this in many ways...

    it could have been a .get() function. or it could have been an () operator. Apparently the designers chose the * and -> operators.


    And you aren't "dereferencing" the iterator... since the iterator is not a pointer, not all iterators are implemented with pointers, but you always (by convention) get at the item an iterator points to by calling it's * operator.

    In C++, if it looks like a duck, and quacks like a duck... it could be a duck, or it could be anything else that has a duck operator.

    The iterator is just an object that has been designed to look like a pointer. it could be a pointer, but it doesn't have to be, it just has to more or less behave like one.
    Last edited by OReubens; June 15th, 2012 at 02:45 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured