类声明后的分号括号

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

class MyClass
{
.
.
.
} MyInstance;

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

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

48460 次浏览

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.

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.

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.

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.

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

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

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.

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 */
}

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.