关于c++中if(变量)的问题

浏览:34日期:2023-05-26

问题描述

template <class T>Node<T>* BinaryTree<T>::getFather(Node<T>* current){ Node<T>* temp = root; while(temp != NULL || !traverseStack.isEmpty()) {if(temp){ if(temp->leftChild == current || temp->rightChild == current) {return temp; } if(temp->rightChild) {traverseStack.pushStack(temp->rightChild); } temp = temp->leftChild;}else{ traverseStack.popStack(temp);} } return NULL;}

关于c++中if(变量)的问题关于c++中if(变量)的问题

而我已经用 if(temp),程序中temp为无法读取,那就不应该进入if语句,为什么程序还是执行if后面的语句了?

问题解答

回答1:

无法读取是指针异常,不代表指针为0

回答2:

你的root没有初始化吧。temp的值是一个地址值,只要地址值不为0,if都会通过。

回答3:

信息量太少了 不好判断邓俊辉的数据结构? 这种书都有源代码的 去官网上下NULL现在推荐使用nullptr

相关文章: