问题
I have to create simple List implementation. They guy who wants that put struct before member next of class Node. Why is there a struct word, what would be the difference without it?
struct Node{
int value;
struct Node *next;//what is this struct for?
};
struct List{
struct Node *first, *last;
};
回答1:
In your example, there is no need to use the struct keyword before the next declaration. It is usually considered a throw-back from C, where it is required. In C++, this would suffice:
struct Node{
int value;
Node *next;
};
However, if you had a member called Node, then you would have to use struct or class:
struct Node{
int Node;
struct Node *next; // struct or class required here
};
You would also require struct of class for a declaration of a type that is not yet defined (a forward declaration). For example
struct Foo {
class Bar* bar_; // Bar defined later
};
where I used class to show it makes no difference in this scenario.
回答2:
There is no need for struct before next.
That should be a simple pointer to Node object.
来源:https://stackoverflow.com/questions/23501237/what-is-the-difference-between-struct-and-lack-of-struct-word-before-member