结构成员之前"struct"和缺少"struct"单词有什么区别

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

本文关键字:struct 单词 区别 什么 成员 结构      更新时间:2023-10-16

我必须创建一个简单的列表实现。他们想把struct放在Node类的成员next前面。为什么有一个struct字,没有它会有什么不同?

struct Node{
    int value;
    struct Node *next;//what is this struct for?
};

struct List{
    struct Node *first, *last;
};

在您的示例中,不需要在next声明之前使用struct关键字。它通常被认为是C语言的回退,在C语言中需要它。在c++中,这就足够了:

struct Node{
    int value;
    Node *next;
};
然而,如果你有一个名为Node的成员,那么将不得不使用structclass:
struct Node{
    int Node;
    struct Node *next; // struct or class required here
};

对于尚未定义的类型声明(前向声明),还需要structclass。例如

struct Foo {
    class Bar* bar_; // Bar defined later
};

我使用class来显示它在这个场景中没有区别。

next之前不需要struct。这应该是一个指向Node对象的简单指针。