2010-10-31 71 views
0

鑑於以下代碼:C++ - 關於產品的typedef

typedef struct IntElement 
{ 
    struct IntElement *next; // use struct IntElement 
    int   data; 
} IntElement; 

typedef struct IntElement 
{ 
    IntElement *next; // Use IntElement 
    int   data; 
} IntElement; // why I can re-use IntElement here? 

我使用上面的數據結構來定義一個鏈表節點。

  1. 哪個更好?
  2. 爲什麼我可以使用重複的名稱(即typedef結尾的struct IntElement和IntElement)?

回答

8

也不是C++。第一個聲明是舊C語法。其次是有人意識到你不需要使用C++,然後在一行後忘記它。 C++做這樣的:

struct IntElement { 
    IntElement* next; 
    int data; 
}; 
2

@ Q1:無論 C++。只需使用

struct IntElement { 
    IntElement *next; 
    int data; 
}; 

struct IntElement自動爲typedef倒是給IntElement