Why is ; used at the end of class definition.

V

Victor Bazarov

gopal said:
Why is ; ~ semi-colon used at the end of class definition ?

It's a declaration statement. Like any statement it should
end in a semicolon. Why do you put a semicolon after 'a' here:

enum A { one, two };

?

V
 
J

Jerry Coffin

Why is ; ~ semi-colon used at the end of class definition ?

Because the syntax allows you to define an instance of that type,
something like:

class X {
// ...
} x;

So, without the semicolon, the compiler assumes the next symbol it sees
should be the name of an instance. The semicolon tells it that the
definition is complete, and it shouldn't be looking for an instance
name.
 
G

ghostdev85

gopal said:
Why is ; ~ semi-colon used at the end of class definition ?

Thanks JK
C++ uses ; to tokenize the strings.
This means that until the C++ compiler sees ';'
The same goes for english, when we sees full stop, we know its the end
of that phrase.
Hope this helps!!!
 
J

Juha Nieminen

C++ uses ; to tokenize the strings.
This means that until the C++ compiler sees ';'
The same goes for english, when we sees full stop, we know its the end
of that phrase.
Hope this helps!!!

Then why is there no ';' at the end of function implementations?
 
R

Robert Bauck Hamar

No, the ; is a token. C++ uses it as a syntactic element.

Then what? You can write a lot of code without the use of any ';'.
Then why is there no ';' at the end of function implementations?

Short answer: Because the syntax says so. A language's syntax rules are
different from language to language. A lot of languages have 'then' as a
keyword used with 'if'. Some languages use ':=' for assignment while others
use '<-'.

Much of the syntax for C++ is inherited from C, where 'struct' declarations
has a ';', and function definitions does not. In C++, the syntax for
classes is the same as for structs, except for the keyword.

Another answer:

class foo { }

is a type, and can be used like:

class foo { } a, b;

which also defines a and b to be of type foo.

void bar() { }

is not a type, however.
 
J

JohnQ

Robert Bauck Hamar said:
No, the ; is a token. C++ uses it as a syntactic element.


Then what? You can write a lot of code without the use of any ';'.


Short answer: Because the syntax says so. A language's syntax rules are
different from language to language. A lot of languages have 'then' as a
keyword used with 'if'. Some languages use ':=' for assignment while
others
use '<-'.

Much of the syntax for C++ is inherited from C, where 'struct'
declarations
has a ';', and function definitions does not. In C++, the syntax for
classes is the same as for structs, except for the keyword.

Another answer:

class foo { }

is a type, and can be used like:

class foo { } a, b;

which also defines a and b to be of type foo.

void bar() { }

is not a type, however.

So does the ';' at the end of a class/struct definition make it easier to
parse or is it unnecessary? (I'd tend to think that it is unnecessary since
the brackets are there).

John
 
B

Bo Persson

JohnQ wrote:
:: ::: Juha Nieminen wrote:
:::
:::: (e-mail address removed) wrote:
::::: C++ uses ; to tokenize the strings.
:::
::: No, the ; is a token. C++ uses it as a syntactic element.
:::
::::: This means that until the C++ compiler sees ';'
:::
::: Then what? You can write a lot of code without the use of any ';'.
:::
::::: The same goes for english, when we sees full stop, we know its
::::: the end of that phrase.
::::: Hope this helps!!!
::::
:::: Then why is there no ';' at the end of function
:::: implementations?
:::
::: Short answer: Because the syntax says so. A language's syntax
::: rules are different from language to language. A lot of languages
::: have 'then' as a keyword used with 'if'. Some languages use ':='
::: for assignment while others
::: use '<-'.
:::
::: Much of the syntax for C++ is inherited from C, where 'struct'
::: declarations
::: has a ';', and function definitions does not. In C++, the syntax
::: for classes is the same as for structs, except for the keyword.
:::
::: Another answer:
:::
::: class foo { }
:::
::: is a type, and can be used like:
:::
::: class foo { } a, b;
:::
::: which also defines a and b to be of type foo.
:::
::: void bar() { }
:::
::: is not a type, however.
::
:: So does the ';' at the end of a class/struct definition make it
:: easier to parse or is it unnecessary? (I'd tend to think that it
:: is unnecessary since the brackets are there).

No, it is necessary becase the class definition can optionally be
followed by declarations of variables of the class type. Look at foo
above, where the semicolon isn't terminating the class, but the list
"a, b". The semicolon must be there, even if the list is empty!


Bo Persson
 
R

Robert Bauck Hamar

JohnQ said:
So does the ';' at the end of a class/struct definition make it easier to
parse or is it unnecessary? (I'd tend to think that it is unnecessary
since the brackets are there).

If by 'unnecessary', you mean: Could a compiler parse a syntax without
the ';' following a class definition? Then I believe the answer is yes it
is unnecessary, and that a correct program with this syntax would not be
easier nor harder to parse. But as C++ is a complex beast, I could be
wrong.

The technical syntax bit, however, is that a function definition is a
declaration by itself, while a class specifier (as the class foo { ... }
construct is called in the syntax) is a type specifier. Type specifiers can
also be 'int' or 'double'.

So when 'class foo {}' is parsed, the compiler would expect following tokens
in a same way as if 'int' had been parsed.

And to make it complete, here is an example of a function definition with a
class definition:

struct foo {} bar() {}

This is allowed by the syntax, but C++ has other rules to forbid it. My C
compiler compiled it, however. G++ gave an explanation that 'new types may
not be defined in a return type', which says 'g++ did parse it
successfully, but it will not let it compile'.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top