//fills my vector with pointers.
//(some are pointing places, others are set to nullptr
vector<Tree_NodeT*> xml_trees {Build_Tree_List(program_options->Get_Files())};
//time to print them
for (auto tree = xml_trees.begin(); tree != xml_trees.end(); ++tree){
if (*tree){
(*tree)->Print(std::cout,4);
}
}
//this worked! No Segfaults!
//time to print them again
for (auto tree : xml_trees){
if (tree){
tree->Print(std::cout,4);
}
}
//Crash! Segfault.
爲什麼是第二個循環segfaulting,而第一個循環不是?基於範圍for循環在C++ 11 segfault,但不是與正常for循環
指針取消引用?不知道,只是想出我的屁股 –
澄清,沒有C++ 11的經驗,但爲什麼你不在第二個循環中解除引用? - 我假設你使用C++自動解引用? –
@ AK4749,第二個循環中的'tree'在向量中都是'Tree_NodeT *',在第一個循環中,它更像是一個指向'Tree_NodeT *'的指針。 – chris