开发者

C++ Semicolon after class, struct, enum,... useless but compilers still emit errors? [duplicate]

开发者 https://www.devze.com 2023-04-11 12:02 出处:网络
This question already has answers here: Closed 11 years ago. 开发者_JS百科 Possible Duplicate: In C++ classes, why the semi-colon after the closing brace
This question already has answers here: Closed 11 years ago. 开发者_JS百科

Possible Duplicate:

In C++ classes, why the semi-colon after the closing brace

This is something I have wondered about for quite some time, why do you have to put a semicolon after the closing bracket of a class, struct or enum in c++?

Worse (or the actual question), why does compilers emit errors about it when they could simply issue a warning?

Is it really important to put that semicolon there and if, why?


It's a leftover from C. You can declare variables after cladd/struct/enum declaration. the code:

class A{};
A a;

Is identical to:

class A{} a;

The compiler needs the ; to know you don't want to declare variables after the class declaration.


Is it really important to put that semicolon there and if, why?

Yes. This can be useful sometime.

You can define a struct, and can declare an object as:

struct A
{
  //members
} obj;

obj is an object of type A, and you declare this just as shown above. It is in same way you put semicolon after declaring variables, such as:

int i;                      //declares i of type int
std::string s;              //declares s of type std::string
struct {int x,y,z;} point;  //declares point of an unnamed struct type  

Here first two declarations look very familiar, but the last one looks slightly unfamiliar, as it defines an unnamed struct, and simultaneously declares an object called point of the unnamed struct.

The bottomline is that you put a semicolon after declaring objects uniformly, no matter how you declare the objects, no matter what their types are.

Not only that you can even typedef in a uniform way:

 typedef int my_int;
 typedef std::string my_string;
 typedef struct {int x,y,z;} my_point;

Now all of these my_int, my_string, and my_point are types.

But when you do neither of them (i.e neither declaring objects, nor defining typedefs), then you simply leave the place as such, blank, with no declaration/definition whatsoever. But the point to be noted is that it gives you an interesting and useful feature all along.


Class, struct and enums definitions are that, definitions. And definitions in C++, as in C, must be finished with a semicolon. If you do not put it there would be a lot of ambiguities:

struct X
{
}   //no ;
foo()  //no return value
{
}

This is read as a function foo that returns a struct, not an struct and a function that returns an (ommited) integer. This is valid C, but not C++.

int *x;
void foo()
{
    struct S { }  //no ;
    *x = 3;  // <--- what is this?
}

Is the second x a local variable of type pointer-to-S, or is a reference to the global x? You simply cannot tell.

There are a lot of other examples, probably you can think a few, if you try.


why do you have to put a semi colon after the closing bracket of a class, struct or enum in c++?

Real answer, Because the C++ Standard says so. The language was designed in a way so that every declaration must end with a ;.

An class object can be declared as:

class MyClass
{
    //Members    
}obj;

And hence the ; at the end of the brace.

Worse (or the actual question), why does compilers emit errors about it when they could simply issue a warning?

The compiler issues a warning when it doubts something might be wrong or potentially hazardous

The compiler issues an error when the program does not follow the syntactic guidelines laid out by the standard, leaving out the semi colon in the cases you mentioned violates the syntax rules mentioned by the C++ standard and hence the error.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号