关于c++中if(变量)的问题
ringa_lee
ringa_lee 2017-04-17 13:10:46
0
3
597
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

全部回覆(3)
巴扎黑

無法讀取是指標異常,不代表指標為0

大家讲道理

你的root沒有初始化吧。
temp的值是一個位址值,只要位址值不為0,if都會通過。

Peter_Zhu

資訊量太少了 不好判斷
鄧俊輝的資料結構? 這種書都有原始碼的 去官網上下
NULL現在推薦使用nullptr

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板