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

C++

C++ Problem Overview


In a C++ class declaration:

class Thing
{
    ...
};

why must I include the semicolon?

C++ Solutions


Solution 1 - C++

The full syntax is, essentially,

class NAME { constituents } instances ;

where "constituents" is the sequence of class elements and methods, and "instances" is a comma-separated list of instances of the class (i.e., objects).

Example:

class FOO {
  int bar;
  int baz;
} waldo;

declares both the class FOO and an object waldo.

The instance sequence may be empty, in which case you would have just

class FOO {
  int bar;
  int baz;
};

You have to put the semicolon there so the compiler will know whether you declared any instances or not.

This is a C compatibility thing.

Solution 2 - C++

because you can optionally declare objects

class Thing
{
    ...
}instanceOfThing;

for historical reasons

Solution 3 - C++

A good rule to help you remember where to put semicolons:

  • If it's a definition, it needs a semicolon at the end. Classes, structs and unions are all information for the compiler, so need a trailing ; to mark no declared instances.
  • If it contains code, it doesn't need a semicolon at the end. If statements, for loops, while loops and functions contain code, so don't need a trailing ;.

Namespaces also don't require a trailing semicolon, because they can contain a mix of both the above (so can contain code, so don't need a semicolon).

Solution 4 - C++

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.

The statement a, b, c, d; by itself, in this context, just evaluates a, b, c and d. In this context, that does nothing.

However, if it's right after the struct/class definition (before the ;) it creates 4 instances of the created struct/class, a, b, c and d

Solution 5 - C++

Because it could be a definition of the next element. For example, taking it from C syntax: if you declare

struct { ... } main (int argc, char..

then it assumes main returns a struct. If there was a semicolon,

struct { ... }; main (int argc, char..

then main returns an int.

Solution 6 - C++

Because the language grammar says so...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTarquilaView Question on Stackoverflow
Solution 1 - C++John R. StrohmView Answer on Stackoverflow
Solution 2 - C++jk.View Answer on Stackoverflow
Solution 3 - C++AshleysBrainView Answer on Stackoverflow
Solution 4 - C++DigitalRossView Answer on Stackoverflow
Solution 5 - C++lorenzogView Answer on Stackoverflow
Solution 6 - C++Maximilian MayerlView Answer on Stackoverflow