Connecting Tech Pros Worldwide Forums | Help | Site Map

about the header file and source file in C++

stonny
Guest
 
Posts: n/a
#1: Jun 27 '08
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
Victor Bazarov
Guest
 
Posts: n/a
#2: Jun 27 '08

re: about the header file and source file in C++


stonny wrote:
Quote:
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.
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.
Quote:
What is the reason for this?
A class definition is a declaration statement with the optional variable
name[s] you can place after the curly brace. A function definition is
not a declaration statement. It's a declaration followed by a compound
statement (a block surrounded by curly braces).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Paavo Helde
Guest
 
Posts: n/a
#3: Jun 27 '08

re: about the header file and source file in C++


stonny <zhangdexin2007@gmail.comwrote in news:a4cda246-97f3-407a-a0dc-
2d465f826d78@b64g2000hsa.googlegroups.com:
Quote:
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.
>
This holds only for class definitions, and has nothing to do with header
or source files.
Quote:
What is the reason for this?
Support for inherited C syntax. In C it is common to write things like:

struct {int x, y;} my_point1, my_point2;
typedef struct Point_ {int x, y;} Point;

You see that you need a terminator for ending the whole construct. I
guess C++ could have managed with a syntax without semicolons if started
from scratch. Anonymous structs might come handy still sometimes though.

hth
Paavo
Paavo Helde
Guest
 
Posts: n/a
#4: Jun 27 '08

re: about the header file and source file in C++


Paavo Helde <nobody@ebi.eewrote in news:Xns9A9F588C6EB9Enobodyebiee@
216.196.97.131:
Quote:
stonny <zhangdexin2007@gmail.comwrote in news:a4cda246-97f3-407a-
a0dc-
Quote:
2d465f826d78@b64g2000hsa.googlegroups.com:
>
Quote:
>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.
>>
>
This holds only for class definitions, and has nothing to do with
header
Quote:
or source files.
>
Quote:
>What is the reason for this?
>
Support for inherited C syntax. In C it is common to write things like:
>
struct {int x, y;} my_point1, my_point2;
typedef struct Point_ {int x, y;} Point;
>
You see that you need a terminator for ending the whole construct. I
guess C++ could have managed with a syntax without semicolons if
started
Quote:
from scratch.
I would like to clarify my "I guess" statement. I meant that I believe
C++ syntax would have remained unambigous if the optional identifier
names and ending semicolon would have been dropped from the syntax of the
'class' construct. This would make it either inconsistent with 'struct'
or, if 'struct' were changed likewise, incompatible with C.

Regards
Paavo


Rafael Anschau
Guest
 
Posts: n/a
#5: Jun 27 '08

re: about the header file and source file in C++


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...
Rolf Magnus
Guest
 
Posts: n/a
#6: Jun 27 '08

re: about the header file and source file in C++


Victor Bazarov wrote:
Quote:
stonny wrote:
Quote:
>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.
>
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.

James Kanze
Guest
 
Posts: n/a
#7: Jun 27 '08

re: about the header file and source file in C++


On May 22, 4:38 pm, Rolf Magnus <ramag...@t-online.dewrote:
Quote:
Victor Bazarov wrote:
Quote:
stonny wrote:
Quote:
Sorry about the beginners question
Quote:
Quote:
Quote:
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.
Quote:
Quote:
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.
Quote:
However, I can see how it can be a bit hard to understand
where to put semicolons and why. Take this example:
Quote:
namespace N
{
class C
{
void f()
{
} // (1)
>
}; // (2)
Quote:
void f()
{
} // (3)
} // (4)
Quote:
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
Quote:
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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Closed Thread