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

后端 未结 2 839
北荒
北荒 2020-12-12 05:58

I have to create simple List implementation. They guy who wants that put struct before member next of class Node. Why is there a

2条回答
  •  没有蜡笔的小新
    2020-12-12 06:41

    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.

提交回复
热议问题