C++ - 关于typedef的问题

q09*_*987 0 c++

给出以下代码:

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?
Run Code Online (Sandbox Code Playgroud)

我使用上面的数据结构来定义链表节点.

  1. 哪个更好?
  2. 为什么我可以使用重复的名称(即typedef结束中的struct IntElement和IntElement)?

Pup*_*ppy 8

也不是C++.第一个声明是旧的C语法.第二个是认识到你在C++中不需要它然后在一行之后忘记它的人.C++是这样的:

struct IntElement {
    IntElement* next;
    int data;
};
Run Code Online (Sandbox Code Playgroud)