Here's the code I've been running:
#include <iostream>
#include <list>
using namespace std;
class SayProcess
{
public:
SayProcess(){}
void Say()
{
cout << "Say" << endl;
}
~SayProcess()
{
cout << "Deleted" << endl;
}
};
int main()
{
SayProcess s1;
SayProcess s2;
SayProcess s3;
list<SayProcess> List;
List.push_back(s1);
List.push_back(s2);
List.push_back(s3);
list<SayProcess>::iterator it = List.begin();
for(; it != List.end(); it++)
{
(*it).Say();
List.erase(it);
}
cout << "List Completely Deleted!" << endl;
char x;
cin >> x;
return 0;
}
This is a simplified version of the code that is broken, but it has the same problems. I was hoping to simply have the loop print out each Process's message and then erase the Process from the list, but it's not working the way I had intended.
When run, this code will repeatedly print the SayProcess's two messages, the "Say" function's message and the destructor's message until infinity. The fact that it is repeatedly calling the destructor confuses me. Is it copying the item somehow?
I feel as if the problem is in my understanding of how either lists or iterators work, but I don't know enough about their design to tell why, and MSDN has been unhelpful. Can anyone help out by telling me what I'm doing wrong?






