Re: references and pointers
On Nov 4, 8:08 am, Kira Yamato <kira...@earthlink.net> wrote:
[...]
I'm still a newbie in C++, but I'm getting the sense that
proper use of STL should eliminate all needs of pointers.
Where did you get this idea from? Iterators can only be used
for objects in a sequence---in practice, in a container. Most
objects aren't in a container; entity objects never are, since
they aren't copiable, and it's rare to need a pointer for
anything but an entity object.
However, for efficiency purposes, perhaps pointers should
sometimes be employed, just like 'goto' is theoretically not
needed but may be practically needed.
'goto' is never needed, even practically. Pointers are almost
always needed, even theoretically. (I say almost, because I
believe that there are applications which don't have any
"entity" objects. I can imagine some serious numeric
applications, for example, which use neither pointers nor
iterators, ever.)
Iterators are for iterating. For the most part, you don't want
to maintain iterators (nor pointers to objects in a container)
for any significant duration; adding or removing elements from
the container can invalidate the iterators. STL containers are
designed with value semantics (the only semantics which really
make sense for a container in the context of C++). This means
that objects in the container do not have identity; they may
"move around". It also means that, conceptually, you won't
normally keep pointers, references or iterators designating them
beyond the scope of a simple algorithm or iteration. If you
find yourself doing so, then you're using the wrong tool, or
misusing it.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34