about the header file and source file in C++

S

stonny

Sorry about the beginners question

In the header file, each time you define a class or function, it has
to be followed by a semicolon.
But in the source file, the functions do not have to be followed by
semicolon.

What is the reason for this?

Thanks
 
R

Rafael Anschau

Also, I find that semicolons make the code more readable(which is
always a good thing in C++). If you are reading a mess and notices a
{};(without first seeing where it starts) you know that´s the end of a
class, enum or struct...
 
R

Rolf Magnus

Victor said:
No. There should be no semicolons after a function body anywhere.
Inside a class definition you're allowed to put extraneous semicolons
but you do not have to put them there. Actually, I find the practice of
placing a semicolon after the body of a function defined inside a class
definition, irritating.

However, I can see how it can be a bit hard to understand where to put
semicolons and why. Take this example:

namespace N
{

class C
{
void f()
{
} // (1)

}; // (2)

void f()
{
} // (3)

} // (4)

At (1) you can choose whether to add a semicolon or not
At (2) you must put one
At (3) and (4), it's forbidden

Especially the difference between (1) and (3) is surprising.
 
J

James Kanze

However, I can see how it can be a bit hard to understand
where to put semicolons and why. Take this example:
namespace N
{
class C
{
void f()
{
} // (1)

}; // (2)
void f()
{
} // (3)
} // (4)
At (1) you can choose whether to add a semicolon or not
At (2) you must put one
At (3) and (4), it's forbidden
Especially the difference between (1) and (3) is surprising.

But historically explainable: (3) is according to C; in the case
of (1), Stroustrup felt that this distinction was artificial,
and added an optional semicolon to the grammar.

IMHO, the simplest solution would just be to modify the grammar
so that an empty statement is a declaration, rather than an
expression statement, so that a semicolon may appear anywhere a
declaration may, rather than only where an expression statement
is allowed. Or simply say that an empty statement is an empty
statement (a new type of statement), and that it can occur
anywhere any other statement may occur.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top