类声明大括号后的分号
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
旧版本的 C 具有来自函数的隐式 int 返回类型,除非另有声明。 如果我们省略结构定义末尾的
;
,我们不仅定义了一个新类型fred
,而且还声明了main() 将返回
fred
的实例。 即代码将被解析如下: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:
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 typefred
, but also declaring thatmain()
will return an instance offred
. I.e. the code would be parsed like this:语言要求类型声明中右大括号后面的分号。 从最早的 C 版本开始就是这样。
是的,人们确实会执行您刚刚在那里发布的声明。 它对于在方法内部创建作用域类型很有用。
在这种情况下,我认为为什么需要分号就很清楚了。 至于为什么在头文件中声明的更一般情况下需要它,我不确定。 我的猜测是,它是历史性的,这样做是为了让编译器的编写更容易。
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.
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.
我想这是因为类是声明,即使它们需要大括号进行分组。 是的,有一个历史论点,因为在 C 中你可以做
你应该在 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
you should in C++ be able to do a similar thing, it makes sense for the
class
declaration to behave in the same way.它是
类声明大括号后面的分号的缩写,实际上有些过分,但这就是 C++ 的定义方式。 变量声明后的分号总是需要的并且有意义。
It's short for
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.
我不使用这样的声明
,但在这种情况下我可以理解为什么有分号。
因为它就像
int a;
- 变量声明。可能是为了保持一致性,因为您可以省略“MyInstance”分号。
I do not use such declarations
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.
在 C/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
is the complete statement.
One could define multiple instances of the declared class in a single statement
This is completely consistent with declaring multiple instances of a primitive type in a single statement:
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.
出于兼容性原因,在
struct
之后需要它,您希望这样:It is needed after a
struct
for compatibility reasons, and how would you like this:我们可以定义一个
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 classsomething
. Therefore to declair an object of the class we usesemi-colon
after the class.