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

后端 未结 6 625
旧巷少年郎
旧巷少年郎 2020-12-01 09:22

In a C++ class declaration:

class Thing
{
    ...
};

why must I include the semicolon?

6条回答
  •  隐瞒了意图╮
    2020-12-01 09:44

    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).

提交回复
热议问题