This is the example code:
Node *n1 = new Node();
Node *n2 = new Node();
Node *n3 = new Node();
qWarning() << "Setting node names";
n1->setName("N1");
n2->setName("N3");
n3->setName("N3");
qWarning() << "Creating list";
QList<Node*> list;
list << n1 << n2 << n3;
qWarning() << "List iteration:";
for (int i = 0; i < list.size(); i++){
qWarning() << "Node:" << list.at(i)->getName();
}
if (n1 != NULL){
qWarning() << "N1 EXISTS";
}
else{
qWarning() << "N1 DOES NOT EXIST";
}
qWarning() << "Clearing list, CLEARING EVERYTHING";
//list.clear();
while (list.size() > 0){
Node *n = list.first();
list.removeFirst();
delete n;
}
qWarning() << "Accessing nodes";
if (n1 != NULL){
qWarning() << "N1 EXISTS";
}
else{
qWarning() << "N1 DOES NOT EXIST";
}
The problem is that both times the printed output is N1 EXISTS
. However, clearly, If I try to access n1
after the accessing nodes message, the program will crash. How can I check that the data structure to which n1
points, actually exists?