W
William Payne
Ok, in my program I have a std::list<Document*>, where Document is one of my
own classes.
I need to go through this list and check each item if it's ready for
deletion. If it's not, skip to next, if it's ready I take steps to delete
it. The thing is that the list is part of a gui application and the system
removes the Document* from the list during the execution of the loop body.
That means I cant use iterators to iterate over the list. So I tried using
the size of the list, like this:
while(m_documents.size())
{
Document* ptr = m_documents.front();
if(ptr->ready_for_deletion())
{
ptr->clean_up(); // This removes ptr from the list before next
iteration
}
}
The problem with that solution is that I get an endless loop for Documents
not ready for deletion. I need to determine if I've asked a document if it's
ready for deletion or not and loop until all documents have been deleted or
there are only non-ready documents left in the list. I can't think of a
solution. I could create a new list of only Documents ready for deletion and
then delete those, but that's not really an option because when I find a
document ready for deletion, I need to delete it right now, not a bit later.
Not sure I'm making much sense and maybe I'm off-topic. If so, I apologise.
/ WP
own classes.
I need to go through this list and check each item if it's ready for
deletion. If it's not, skip to next, if it's ready I take steps to delete
it. The thing is that the list is part of a gui application and the system
removes the Document* from the list during the execution of the loop body.
That means I cant use iterators to iterate over the list. So I tried using
the size of the list, like this:
while(m_documents.size())
{
Document* ptr = m_documents.front();
if(ptr->ready_for_deletion())
{
ptr->clean_up(); // This removes ptr from the list before next
iteration
}
}
The problem with that solution is that I get an endless loop for Documents
not ready for deletion. I need to determine if I've asked a document if it's
ready for deletion or not and loop until all documents have been deleted or
there are only non-ready documents left in the list. I can't think of a
solution. I could create a new list of only Documents ready for deletion and
then delete those, but that's not really an option because when I find a
document ready for deletion, I need to delete it right now, not a bit later.
Not sure I'm making much sense and maybe I'm off-topic. If so, I apologise.
/ WP