I have below code. I think all of 3 elements in the vector can be deleted and erased correctly, but the original pointer t1, t2, t3 become dangling pointers, right ? It looks like a problem if they are used in somewhere else. How to deal with them ?
typedef struct test
{
test(int i, int j): a(i), b(j){}
int a;
int b;
}Test;
int main(int argc, char** argv)
{
test* t1 = new test(1, 2);
test* t2 = new test(1, 4);
test* t3 = new test(1, 8);
vector<test*> testList;
testList.push_back(t1);
testList.push_back(t2);
testList.push_back(t3);
for(auto it = testList.begin(); it!=testList.end();)
{
delete (*it);
*it = nullptr;
it = testList.erase(it);
}
return 0;
}