在函数签名中指定结构体

发布于 2024-12-04 03:23:20 字数 198 浏览 1 评论 0原文

说我有

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥抱没勇气 2024-12-11 03:23:20

在 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.

美男兮 2024-12-11 03:23:20

不在您编写的代码中。据我所知,使用带有和不带有 struct 的已定义类名之间的唯一区别如下:

struct mystruct
{
};

void mystruct() {}

void foo(struct mystruct x){} // compiles
void foo(mystruct x){} // doesn't - for compatibility with C "mystruct" means the function

因此,不要定义与类同名的函数。

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:

struct mystruct
{
};

void mystruct() {}

void foo(struct mystruct x){} // compiles
void foo(mystruct x){} // doesn't - for compatibility with C "mystruct" means the function

So, don't define a function with the same name as a class.

烟花易冷人易散 2024-12-11 03:23:20

没有区别。后者是正确的C++语法;前者可以作为恢复 C 程序员的遗留变体。

请注意,structclass 本质上是相同的,并且都定义了一个类,因此 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 and class 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.]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文