所有我做的是聲明指針,然後檢查是否它指向什麼:C++指針永遠不會初始化還是輸出作爲初始化
#pragma once
#include <Node.h>
#include <stdio.h>
template <class NodeDataType>
class LinkedList
{
typedef Node<NodeDataType>* pNode;
private:
pNode pHead;
pNode pTail;
public:
void Add(NodeDataType* pNodeTypeData)
{
if(pHead)
{
printf("phead is initialized\n");
}
else
{
printf("phead is not initialized\n");
}
}
};
然後,在main()
,我想提出一個新的LinkedList對象,並調用Add
但是當我運行它時,它輸出pHead is initialized
? 但我從來沒有初始化它?
有人可以解釋給我嗎?
感謝
(主)
#include <stdio.h>
#include <LinkedList.h>
#include <GameObject.h>
int main(int argc, char** argv)
{
LinkedList<GameObject> meList;
meList.Add(new GameObject());
return 0;
}
可以有幾件事情錯在這裏,請張貼代碼主要還以便我們可以看到自己的錯誤 – Curious
看看這個https://stackoverflow.com/questions/1910832/why-arent-pointers-initialized-with-null-by-default –
它是否輸出'initialized'或'uninitialized '。因爲你永遠不會初始化'pHead',而是在'if(pHead)'中檢查它。 –