开发者

C++ -- Question about typedef [closed]

开发者 https://www.devze.com 2023-01-23 09:45 出处:网络
It's difficult to tell what is b开发者_如何转开发eing asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current for
It's difficult to tell what is b开发者_如何转开发eing asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

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.

  1. Which is better one?
  2. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消