views:

264

answers:

3

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?

+1  A: 

The following is legal in C++:

struct MyStruct
{
} anInstanceOfMyStruct;

struct
{
} anInstanceOfAnUnnamedStruct;

You need the semicolon to indicate you aren't creating a new instance. The language designers of C# and Java apparently didn't feel that allowing this was a useful addition to their language.

Michael
+2  A: 

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;
wrang-wrang
+2  A: 

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.

DigitalRoss