2014-10-06 24 views
0

我想初始化類Dlist的新對象。在聲明新對象後,指針第一個末尾應該是NULL。當我聲明Dlist 臨時然而 - 構造函數沒有被識別,並且編譯器給它們賦值如0x0。我不知道爲什麼構造函數被識別。使用構造函數初始化雙鏈表中的NULL指針

// dlist.h 
class Dlist { 
private: 
// DATA MEMBERS 
struct Node 
{ 
    char data; 
    Node *back; 
    Node *next; 
}; 

Node *first; 
Node *last; 

// PRIVATE FUNCTION 
Node* get_node(Node* back_link, const char entry, Node* for_link); 


public: 

// CONSTRUCTOR 
Dlist(){ first = NULL; last = NULL; } // initialization of first and last 

// DESTRUCTOR 
~Dlist(); 

// MODIFIER FUNCTIONS 
void append(char entry); 
bool empty(); 
void remove_last(); 

//CONSTANT FUNCTIONS 
friend ostream& operator << (ostream& out_s, Dlist dl); 

};   
#endif 

// implementation file 
int main() 
{ 
Dlist temp; 
char ch; 

cout << "Enter a line of characters; # => delete the last character." << endl 
<< "-> "; 


cin.get(ch); 
temp.append(ch); 

cout << temp; 
return 0; 
} 
+0

你知道NULL和0x0是相同的值嗎? – 2014-10-06 03:12:49

+1

'0x0'意味着零,這通常是用來表示空的地址。 – 2014-10-06 03:13:15

+0

你是什麼意思構造函數不被識別?你不能在調試器中進入它? – 2014-10-06 03:17:58

回答

1

0x0是NULL。此外,類成員的初始化被更有效地通過構造的初始化列表完成:

Dlist() 
    : first(nullptr) 
    , last(nullptr) 
{ /* No assignment necessary */ } 

當一個類被構造,初始化列表被施加到執行構造的主體之前的對象獲取的存儲器。

+1

那麼,技術上'NULL'不需要* C *規範*零*雖然需要*比較等於0。 – cdhowie 2014-10-06 03:33:48

相關問題