我们声明结构体的两种方式有什么区别? [英] What is the difference between the two ways we declared a struct?

查看:32
本文介绍了我们声明结构体的两种方式有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为 C++ 的初学者,我正在学习链表和其他数据结构.在网上看了一些实现之后,我发现他们定义结构的这两种方式.两者有什么区别.一种是在 next 指针前添加struct",另一种是不添加.

As a beginner with C++ I was learning about linked list and other data structures. After looking at a few implementations online I found these two ways they defined struct. What is the difference between the two. In one we add "struct" before the next pointer, and in one we don't.

方式一:

struct node
{
   int data;
   node *next;
};

方式 2:

struct node
{
   int data;
   struct node *next;
};

推荐答案

struct node *next;

仅在 C 代码中是必需的.在 C 中,做:

Is only necessary in C code. In C, doing:

node *next;

不允许.但是,在 C++ 中,您可以同时使用这两种方法.在这种情况下,它们之间没有区别.

Is not allowed. However, in C++, you can use both methods. There is no difference between them in this case.

您需要在 C++ 中使用 struct 关键字进行声明的唯一情况是,如果存在某种歧义或 struct 尚未定义.(第一个示例是 stat 函数,它是 POSIX 系统上的一个函数一个 struct.

The only time you would need the struct keyword in C++ for a declaration would be if there is some sort of ambiguity or if the struct has not been defined yet. (An example for the first would the stat function, which is a function and a struct on POSIX systems).

这篇关于我们声明结构体的两种方式有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆