类和结构后面的分号

发布于 2024-08-13 02:10:16 字数 540 浏览 8 评论 0原文

可能的重复:
为什么我必须放C++ 中类声明末尾的分号?

找到 重复,请投票关闭。

为什么在 C++ 中类和结构必须用分号结束?

就像下面的代码一样:

class myClass
{



};

struct muStruct
{

};

这种语法在 Java 或 C# 中不是必需的。为什么 C++ 解析器需要它?

Possible Duplicate:
Why must I put a semicolon at the end of class declaration in C++?

Found duplicate, vote to close please.

Why do classes and structs have to be concluded with semicolon in C++?

Like in the following code:

class myClass
{



};

struct muStruct
{

};

This syntax isn't necessary in Java or C#. Why does the C++ parser need it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

送你一个梦 2024-08-20 02:10:16

这就是为什么...

int a,b,c,d;
int main(void) {
    struct y {
  }; a, b, c, d;
    struct x {
  } a, b, c, d;
}

两条不同的语句,两种完全不同的含义,都是合法的C/C++,唯一的区别就是struct声明后的;

This is why...

int a,b,c,d;
int main(void) {
    struct y {
  }; a, b, c, d;
    struct x {
  } a, b, c, d;
}

Two different statements, two completely different meanings, both legal C / C++, and the only difference is the ; after the struct declaration.

两相知 2024-08-20 02:10:16

因为 } 后面的下一个单词可能声明该类型的变量。这是 C 的遗物:

struct S { int a; } s;
s.a;

Because the next word following the } may declare a variable of said type. This is a relic from C:

struct S { int a; } s;
s.a;
蘑菇王子 2024-08-20 02:10:16

以下内容在 C++ 中是合法的:

struct MyStruct
{
} anInstanceOfMyStruct;

struct
{
} anInstanceOfAnUnnamedStruct;

您需要分号来指示您不创建新实例。 C# 和 Java 的语言设计者显然并不认为允许这样做对他们的语言来说是一个有用的补充。

The following is legal in C++:

struct MyStruct
{
} anInstanceOfMyStruct;

struct
{
} anInstanceOfAnUnnamedStruct;

You need the semicolon to indicate you aren't creating a new instance. The language designers of C# and Java apparently didn't feel that allowing this was a useful addition to their language.

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