What is the difference between quot;structquot; and lack of quot;structquot; word before member of a struct(“struct和“struct有什么区别?并且缺乏“结构结构体成员之前的词)
问题描述
我必须创建简单的 List 实现.他们想要将 struct 放在 Node 类的成员 next 之前.为什么有struct这个词,没有它有什么区别?
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;
};
推荐答案
在您的示例中,无需在 next 声明之前使用 struct 关键字.在需要时,它通常被认为是 C 的回退.在 C++ 中,这就足够了:
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;
};
但是,如果您有一个名为 Node 的成员,那么您将必须使用 struct 或 class:
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
};
对于尚未定义的类型的声明(前向声明),您还需要 class 的 struct.例如
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
};
我使用 class 来显示它在这种情况下没有区别.
where I used class to show it makes no difference in this scenario.
这篇关于“struct"和“struct"有什么区别?并且缺乏“结构"结构体成员之前的词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“struct"和“struct"有什么区别?并且缺乏“结构"结构体成员之前的词
基础教程推荐
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- c++ STL设置差异 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
