Iterators...

A

Alan Johnson

Vij said:
How can I check if an iterator is still valid and can be still
dereferenced ?

There is no (portable) way.

If you need to hand out handles to an object that might suddenly
disappear, you need to add an extra level of indirection. That is,
create a reference counted object that won't disappear until all the
references to it are gone, and have that object contain a pointer to the
real object.

For a well designed and widely tested implementation of such an idea,
see boost::shared_ptr and boost::weak_ptr.
<url: http://boost.org/libs/smart_ptr/smart_ptr.htm>
 
A

Alan Johnson

Vij said:
What is the non protable way to test this. (if there indeed is any way
?)

Sorry, I just added that because without it someone would almost
certainly come along saying, "But, on platform X you can do Y!" The
truth is that in most implementations I've seen there isn't any way at all.
 
B

benben

Vij said:
How can I check if an iterator is still valid and can be still
dereferenced ?

By analyzing the code you write. Usually, if you are not so aware of the
validity of a particular iterator, abandon it and ask for a valid one.
This is because iterators are more like handle to a position in a
container instead of an object.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

2 Iterators/Iterator Math 1
sorts and iterators 10
initializing an array from iterators 1
set of iterators 5
iterators 16
A question about iterators 5
Iterators on Vector 3
sampling using iterators 4

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top