关于c++中if(变量)的问题
ringa_lee
ringa_lee 2017-04-17 13:10:46
0
3
567
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后面的语句了?

ringa_lee
ringa_lee

ringa_lee

reply all(3)
巴扎黑

Unable to read is a pointer exception, which does not mean that the pointer is 0

大家讲道理

Your root has not been initialized.
The value of temp is an address value. As long as the address value is not 0, if will pass.

Peter_Zhu

The amount of information is too small and it is difficult to judge
Deng Junhui’s data structure? This kind of book has source code. Go to the official website
NULL now recommends using nullptr

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template