类声明大括号后的分号

发布于 2024-07-18 03:01:07 字数 293 浏览 6 评论 0原文

在 C++ 类中,为什么右大括号后面有分号? 我经常忘记它并出现编译器错误,因此浪费了时间。 对我来说似乎有些多余,但事实并非如此。 人们真的会做这样的事情吗:

class MyClass
{
.
.
.
} MyInstance;

我从结构和枚举的 C 兼容性角度得到它,但由于类不是 C 语言的一部分,我猜它主要是为了保持类似声明结构之间的一致性。

我所寻找的更多的是与设计原理相关,而不是能够改变任何东西,尽管一个好的代码完成 IDE 可能会在编译之前捕获这个问题。

In C++ classes, why the semi-colon after the closing brace? I regularly forget it and get compiler errors, and hence lost time. Seems somewhat superfluous to me, which is unlikely to be the case. Do people really do things like:

class MyClass
{
.
.
.
} MyInstance;

I get it from a C compatibility point of view for structs and enums, but since classes aren't part of the C language I guess it's primarily there the keep consistency between similar declaration constructs.

What I was looking for was more related to design rationale rather than being able to change anything, although a good code completion IDE might trap this before compilation.

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

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

发布评论

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

评论(8

执妄 2024-07-25 03:01:07
struct fred { int x; long y; }; 
main() 
{ 
  return 0; 
} 

旧版本的 C 具有来自函数的隐式 int 返回类型,除非另有声明。 如果我们省略结构定义末尾的 ;,我们不仅定义了一个新类型 fred,而且还声明了 main() 将返回 fred 的实例。 即代码将被解析如下:

struct fred { int x; long y; } main()
{ 
  return 0; /* invalid return type, expected fred type */
} 

The link provided by @MichaelHaren appears to provide the root cause. The semicolon (as others have pointed out) is inherited from C. But that doesn't explain why C used it in the first place. The discussion includes this gem of an example:

struct fred { int x; long y; }; 
main() 
{ 
  return 0; 
} 

Older versions of C had an implicit int return type from a function unless declared otherwise. If we omit the ; at the end of the structure definition, we're not only defining a new type fred, but also declaring that main() will return an instance of fred. I.e. the code would be parsed like this:

struct fred { int x; long y; } main()
{ 
  return 0; /* invalid return type, expected fred type */
} 
神爱温柔 2024-07-25 03:01:07

语言要求类型声明中右大括号后面的分号。 从最早的 C 版本开始就是这样。

是的,人们确实会执行您刚刚在那里发布的声明。 它对于在方法内部创建作用域类型很有用。

void Example() {
  struct { int x; } s1;
  s1.x = 42;

  struct ADifferentType { int x; };
}

在这种情况下,我认为为什么需要分号就很清楚了。 至于为什么在头文件中声明的更一般情况下需要它,我不确定。 我的猜测是,它是历史性的,这样做是为了让编译器的编写更容易。

The semi-colon after the closing brace in a type declaration is required by the language. It's been that way since the earliest versions of C.

And yes, people do indeed do the declaration you just put up there. It's useful for creating scoped types inside of methods.

void Example() {
  struct { int x; } s1;
  s1.x = 42;

  struct ADifferentType { int x; };
}

In this case, I think it's clear why the semi-colons are needed. As to why it's needed in the more general case of declaring in the header file I'm unsure. My guess is that it's historical and was done to make writing the compiler easier.

红颜悴 2024-07-25 03:01:07

我想这是因为类是声明,即使它们需要大括号进行分组。 是的,有一个历史论点,因为在 C 中你可以做

struct
{
  float x;
  float y;
} point;

你应该在 C++ 中做类似的事情,所以 class 声明以相同的方式运行是有意义的。

I guess it's because classes are declarations, even when they need braces for grouping. And yes, there's the historical argument that since in C you could do

struct
{
  float x;
  float y;
} point;

you should in C++ be able to do a similar thing, it makes sense for the class declaration to behave in the same way.

黑凤梨 2024-07-25 03:01:07

它是

class MyClass
{
.
.
.
};

// instance declaration
MyClass MyInstance;  // semicolon here

类声明大括号后面的分号的缩写,实际上有些过分,但这就是 C++ 的定义方式。 变量声明后的分号总是需要的并且有意义。

It's short for

class MyClass
{
.
.
.
};

// instance declaration
MyClass MyInstance;  // semicolon here

The semicolon after the curly braces of the class declaration is actually overkill, but it is how C++ is defined. The semicolon after the variable declaration is always needed and makes sense.

与风相奔跑 2024-07-25 03:01:07

我不使用这样的声明

class MyClass
{
.
.
.
} MyInstance;

,但在这种情况下我可以理解为什么有分号。
因为它就像 int a; - 变量声明。

可能是为了保持一致性,因为您可以省略“MyInstance”分号。

I do not use such declarations

class MyClass
{
.
.
.
} MyInstance;

But in this case I can understand why is semicolon there.
Because it is like int a; - variable declaration.

Probably for consistence as you can omit 'MyInstance' semicolon stays there.

醉梦枕江山 2024-07-25 03:01:07

在 C/C++ 中; 是一个语句终止符。
所有语句均以 ; 终止 以避免歧义(并简化解析)。
语法在这方面是一致的。
即使类声明(或任何与此相关的块)
有多行长并且是
用 {} 分隔,它仍然只是一个语句({ } 是语句的一部分)
因此需要以 ; 终止 ( ; 不是分隔符/定界符)

在您的示例中

class MyClass{...} MyInstance;

是完整的语句。
人们可以在一条语句中定义所声明类的多个实例

class MyClass{...} MyInstance1, MyInstance2;

这与在一条语句中声明基元类型的多个实例完全一致:

int a, b, c;

人们不经常看到类和实例的这种声明的原因是实例只能? 是一个全局变量,并且您并不经常需要全局对象,除非它们是静态和/或普通旧数据结构。

In C/C++ the ; is a statement terminator.
All statements are terminated with ; to avoid ambiguity (and to simplify parsing).
The grammar is consistent in this respect.
Even though a class declaration (or any block for that matter)
is multiple lines long and is
delimited with {} it is still simply a statement (the { } is part of the statement)
hence needs to be terminated with ; (The ; is not a separator/delimitor)

In your example

class MyClass{...} MyInstance;

is the complete statement.
One could define multiple instances of the declared class in a single statement

class MyClass{...} MyInstance1, MyInstance2;

This is completely consistent with declaring multiple instances of a primitive type in a single statement:

int a, b, c;

The reason one does not often see such desclaration of class and instance, is the instance could ?only? be a global variable, and you don't really often want global objects unless they are static and/or Plain Old Data structures.

南风几经秋 2024-07-25 03:01:07

出于兼容性原因,在 struct 之后需要它,您希望这样:

struct MyStruct { ... };
class  MyClass  { ... }    //inconsistency

It is needed after a struct for compatibility reasons, and how would you like this:

struct MyStruct { ... };
class  MyClass  { ... }    //inconsistency
梦途 2024-07-25 03:01:07

我们可以定义一个class Something {...}accum, trans;,其中accum和trans可以是something类的两个对象。 因此,为了声明类的对象,我们在类后面使用分号。

We can define a class something {...}accum, trans; where accum and trans can be two objects of the class something. Therefore to declair an object of the class we use semi-colon after the class.

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