On Jun 22, 11:55 pm, "xgn...@gmail.c om" <xgn...@gmail.c omwrote:
Suppose we have a vector:
vector<intvec(1 0);
We can declare a iterator this way:
vector<int>::it erator vecItor;
Modulo using a typedef, that's the only way to declare an
iterator.
and then dereference it like this:
for (vecItor = vec.begin(); vecItor != vec.end(); vecItor++)
{
cout << *vecItor << endl;
}
But how can we dereference the iterator if we declare it this way:
vector<int>::it erator* vecItor;
That doesn't declare an iterator. It declares a pointer to an
iterator. Given that iterators have value semantics in C++, and
generally are designed to have short and restricted livetimes
(since various modifications to the underlying container can
invalidate them), it's hard to see where this would be
appropriate. Do you really want to have to write things like:
std::vector< int >::iterator* i
= new std::vector< int >::iterator( vec.begin() ) ;
while ( *i != vec.end() ) {
// ...
delete i ;
(I need to declare the iterator the second way since I'm declaring it
under a managed C++ class.)
Ask in a group about managed C++. I'm not familiar with it, but
I don't think that they would have C++ in the name if it didn't
support value types.
--
James Kanze (Gabi Software) email:
ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34