F
fungus
I'm moving some code from VC++ 6 to VC++ 2005 and
I've run into a nasty problem because iterators
are no longer pointers.
In the program I'm moving, there's a std::vector
of items hidden inside a class and users of the
class get to iterate it via a const_iterator.
eg.
class foo {
typedef std::vector<bar>list_type;
list_type list;
public:
typedef list_type::const iterator iterator;
iterator begin() { return list.begin(); }
iterator end() { return list.end(); }
};
Then:
for (foo::iterator i=myFoo.begin(); i!=myFoo.end(); ++i) {
// ...
}
Ok, the problem:
Class foo defines a function "erase" to remove a
bar from the list, ie.:
class foo {
...
void erase(iterator i) {
}
};
How can I call std::vector::erase() if I only have
a const iterator? When iterators were pointers I
could just do a const_cast and it worked. With the
new style iterators there's no way to convert a
const iterator to non-const.
There's also no conversion of pointers to iterators,
no addition of values to iterators ("list.begin()+x"),
no nothing!
I'm stumped....
--
<\___/>
/ O O \
\_____/ FTB. For email, remove my socks.
We’re judging how a candidate will handle a nuclear
crisis by how well his staff creates campaign ads.
It’s a completely nonsensical process.
I've run into a nasty problem because iterators
are no longer pointers.
In the program I'm moving, there's a std::vector
of items hidden inside a class and users of the
class get to iterate it via a const_iterator.
eg.
class foo {
typedef std::vector<bar>list_type;
list_type list;
public:
typedef list_type::const iterator iterator;
iterator begin() { return list.begin(); }
iterator end() { return list.end(); }
};
Then:
for (foo::iterator i=myFoo.begin(); i!=myFoo.end(); ++i) {
// ...
}
Ok, the problem:
Class foo defines a function "erase" to remove a
bar from the list, ie.:
class foo {
...
void erase(iterator i) {
}
};
How can I call std::vector::erase() if I only have
a const iterator? When iterators were pointers I
could just do a const_cast and it worked. With the
new style iterators there's no way to convert a
const iterator to non-const.
There's also no conversion of pointers to iterators,
no addition of values to iterators ("list.begin()+x"),
no nothing!
I'm stumped....
--
<\___/>
/ O O \
\_____/ FTB. For email, remove my socks.
We’re judging how a candidate will handle a nuclear
crisis by how well his staff creates campaign ads.
It’s a completely nonsensical process.