在函数签名中指定结构体
说我有
struct mystruct
{
};
之间有区别吗
void foo(struct mystruct x){}
:和
void foo(mystruct x){}
?
Say I have
struct mystruct
{
};
Is there a difference between:
void foo(struct mystruct x){}
and
void foo(mystruct x){}
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 C 中,后者无效。
然而在 C++ 中它们几乎是相同的:如果您还没有声明您的结构,第一个将是有效的,它会将其视为参数的前向声明。
In C the latter isn't valid.
However in C++ they're almost the same: The first one would be valid if you haven't yet declared your struct at all, it would treat it as a forward declaration of the parameter all in one.
不在您编写的代码中。据我所知,使用带有和不带有 struct 的已定义类名之间的唯一区别如下:
因此,不要定义与类同名的函数。
Not in the code you've written. The only difference I know of between using a defined class name with and without
struct
is the following:So, don't define a function with the same name as a class.
没有区别。后者是正确的C++语法;前者可以作为恢复 C 程序员的遗留变体。
请注意,
struct
和class
本质上是相同的,并且都定义了一个类,因此 C++ 中对 C 风格的 POD 结构体没有特殊处理。[编辑:显然有细微的差别,请参阅 Mark B 的出色回答。]
No difference. The latter is the correct C++ syntax; the former is permissible as a legacy variant for recovering C programmers.
Note that
struct
andclass
are essentially the same and both define a class, so there's no special treatment for C-style POD structs in C++.[Edit: Apparently there is a small difference, see Mark B's excellent answer.]