In a C++ class declaration:
class Thing
{
...
};
why must I include the semicolon?
In a C++ class declaration:
class Thing
{
...
};
why must I include the semicolon?
because you can optionally declare objects
class Thing
{
...
}instanceOfThing;
for historical reasons
The full syntax is, essentially,
class NAME { constituents } instances ;
where constituent-sequence is the sequence of class elements and methods, and instance-sequence is a comma-separated list of instances of the class.
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.
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.
A good rule to help you remember where to put semicolons:
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).