Alex Rivera | Logout

Semicolon after classes and structs

Asked 2009-11-23T22:59:06.490
11

Possible Duplicate:
Why must I put a semicolon at the end of class declaration in C++?

Found duplicate, vote to close please.

Why do classes and structs have to be concluded with semicolon in C++?

Like in the following code:

class myClass
{



};

struct muStruct
{

};

This syntax isn't necessary in Java or C#. Why does the C++ parser need it?

Edit
Report

2 Answers

12

This is why...

int a,b,c,d;
int main(void) {
    struct y {
  }; a, b, c, d;
    struct x {
  } a, b, c, d;
}

Two different statements, two completely different meanings, both legal C / C++, and the only difference is the ; after the struct declaration.

answered 2009-11-23T23:06:24.167
10

Because the next word following the } may declare a variable of said type. This is a relic from C:

struct S { int a; } s;
s.a;
answered 2009-11-23T23:03:56.230

Your Answer