What is the difference between “struct” and lack of “struct” word before member of a struct

旧城冷巷雨未停 提交于 2020-01-19 18:05:18

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!