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

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

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;
}

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


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


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


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

【热门文章】
【热门文章】