this book hasnt gone into pointers though it has covered references. |
Good book. References are much more fundamental.
the book says derefencing gives an lvalue |
Good book. That's exactly what it does.
Why do I have to deference this to make it work? |
You want to access the object that is referenced by the iterator. To do exactly that, you dereference.
What exactly does dereferencing normally do? |
It accesses the object that's referenced by the iterator or pointer. There's simply no other way to get to that object.
Why on other sites do they say pointers are a type of iterator? |
Because it is true. A raw pointer to an element of a C array or one-past-the-end of a C array is a random access iterator. Also, vector iterators, string iterators, and C++ array iterators are implemented as raw pointers (except in some debug mode builds)
Lastly, object->member just a synonym of (*object).member and nothing more? |
It is true for built-in meaning of operator-> and operator*. Those operators may be redefined, but they are usually redefined in the way that maintains this identity.
an lvalue Which i understand is a nontemporary object. |
No. Objects are *never* l/r/x-values. That's an expression category.. but it will take a while to explain. For simplicity, imagine the book said "reference" instead. Dereferencing an iterator produces a reference to the object that iterator is pointing to.
So when you use an iterator does it act as a reference to the vector? |
It holds a reference to an element of the vector.
Because I know you need lvalues to access data because of scoping issues. |
This doesn't quite make sense. You can access members of an rvalue just the same.
So does this essentially make a copy of the object in memory for the purpose of printing to the screen? aka an lvalue? |
No, this *essentially* gives you the reference to the pointed-to Person (which is not an object, and not a copy of an object, it's an alias, or an access path to reach an existing object. A read-only access path in your case, since you dereferenced a const_iterator. )Then you use a member access operator to create a reference to the member (name or age) of the pointed-to Person. Then you pass that reference to operator<< for output. No copies involved.