C++ 类声明大括号后的分号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/785686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:17:02  来源:igfitidea点击:

Semicolon after class declaration braces

c++classoopdeclaration

提问by SmacL

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:

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

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.

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

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.

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

采纳答案by JaredPar

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.

语言需要类型声明中右大括号后的分号。自 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 guessis that it's historical and was done to make writing the compiler easier.

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

回答by Nathan

The linkprovided 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:

所述链接由@MichaelHaren提供似乎提供的根本原因。分号(正如其他人指出的那样)是从 C 继承的。但这并不能解释为什么 C 一开始就使用它。讨论包括这个例子的宝石:

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:

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

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

回答by unwind

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

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

struct
{
  float x;
  float y;
} point;

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

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

回答by Stefan Steinegger

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.

类声明的大括号后面的分号实际上是多余的,但它是 C++ 的定义方式。变量声明后的分号总是需要的并且有意义。

回答by Mykola Golubyev

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.

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

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

可能是为了一致性,因为您可以省略 'MyInstance' 分号留在那里。

回答by Zifre

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

struct出于兼容性原因,在 a 之后需要它,您希望如何:

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

回答by Roger Nelson

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)

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

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.

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