Given the following code:
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?
I use above data structure to define a linked-list node.
- Which is better one?
- Why I can use duplicated name (i.e. struct IntElement and IntElement in the typedef end)?
Neither is C++. The first declaration is old C syntax. The second is someone who realized that you don't need that in C++ and then forgot it one line later. C++ does it like this:
struct IntElement {
IntElement* next;
int data;
};
@Q1: Neither in C++. Just use
struct IntElement {
IntElement *next;
int data;
};
struct IntElement
is automatically typedef
'd to IntElement
精彩评论