You have to initialized the pointers to NULL first.
You will be wise to use a container class istead of a naked pointer too.
something like :
QVector<ClassUser*> vecUsers;
//When intilaizing you do (probably in a for loop):
vecUsers.push_back(new ClassUser());
//when cleaning up:
for(int i=0; i<vecUsers.size(); i++) {//some like to use 'foreach'
if(vecUsers.at(i))
delete vecUsers[i];
}
vecUsers.clear();
QVector<ClassUser*> vecUsers;
//When intilaizing you do (probably in a for loop):
vecUsers.push_back(new ClassUser());
//when cleaning up:
for(int i=0; i<vecUsers.size(); i++) {//some like to use 'foreach'
if(vecUsers.at(i))
delete vecUsers[i];
}
vecUsers.clear();
To copy to clipboard, switch view to plain text mode
note in the code above there is no need to initialize to NULL, since the vector is empty when there are not items in it,and any item in it is either NULL if new failed, or a valid pointer.
This approach is good if you just need to hold the pointer array and use it.
If this has to be a dynamic array that gets and removes items, a different approach might be needed, but that depends on your application internals.
Bookmarks