Semi Colon after binding class declaration

Sorry in advance, which is probably a dumb question, but in C ++ classes, why the semicolon after the closing curly brace? I regularly forget about it, get compiler errors, and therefore lose time. It seems to me somewhat unnecessary, which is unlikely to be the case. People really do something like

class MyClass { . . . } MyInstance; 

Edit: I get it from the C compatibility point for structures and enumerations, but since classes are not part of the C language, I assume that in the first place there is consistency between similar declaration constructions, What I was looking for was more related to the design rationale and not with the ability to change anything, although a good code completion IDE may prevent this from happening before compilation.

+65
c ++ oop class declaration semicolon
Apr 24 '09 at 12:47
source share
7 answers

A half-mouth after closing a curly bracket in a type declaration is required by the language. This is the case from the earliest versions of C.

And yes, people really do the declaration that you just put there. It is useful for creating object types inside methods.

 void Example() { struct { int x; } s1; s1.x = 42; struct ADifferentType { int x; }; } 

In this case, I think it’s understandable why half-columns are needed. Regarding the need for a more general case of declarations in the header file, I'm not sure. I suppose this was historical, and this was done to make compiler writing easier.

+40
Apr 24 '09 at 12:50
source share

The link provided by @MichaelHaren seems to be the main reason. The semicolon (as others have pointed out) inherits from C. But this does not explain why C used it in the first place. The discussion includes this pearl example:

 struct fred { int x; long y; }; main() { return 0; } 

Older versions of C had an implicit return type of int from a function, unless otherwise specified. If we omit ; at the end of the structure definition, we not only define the new type fred , but also declare that main() will return an instance of fred . Those. The code will be parsed as follows:

 struct fred { int x; long y; } main() { return 0; /* invalid return type, expected fred type */ } 
+52
May 22 '14 at 14:53
source share

I assume that classes are declarations, even if they need brackets for grouping. And yes, there is a historical argument that since in C you could do

 struct { float x; float y; } point; 

you should be able to do a similar thing in C ++, it makes sense for a class declaration to behave the same.

+15
Apr 24 '09 at 12:51
source share

This is short for

 class MyClass { . . . }; // instance declaration MyClass MyInstance; // semicolon here 

The semicolon after the curly braces of the class declaration is actually crowded, but it's like C ++. A semicolon after a variable declaration is always necessary and makes sense.

+8
Apr 24 '09 at 12:52
source share

I do not use such ads

 class MyClass { . . . } MyInstance; 

But in this case, I can understand why the semicolon. Because it's like int a; - declaration of a variable.

Probably for consistency since you can omit the semicolon "MyInstance" there.

+3
Apr 24 '09 at 12:53
source share

This is needed after the struct for compatibility reasons and how you like it:

 struct MyStruct { ... }; class MyClass { ... } //inconsistency 
+3
Apr 24 '09 at 2:30 p.m.
source share

In C / C ++ the; is the terminator of the statement. All statements cease; to avoid ambiguity (and simplify parsing). In this regard, grammar is consistent. Although the class declaration (or any block, for that matter) has a length of several lines and with the help of {} it is still just a statement ({} is part of the instruction) therefore, it is necessary to stop; (Not a delimiter / delimiter)

In your example

 class MyClass{...} MyInstance; 

- full statement. You can define multiple instances of a declared class in a single expression

 class MyClass{...} MyInstance1, MyInstance2; 

This is consistent with declaring multiple instances of a primitive type in a single expression:

 int a, b, c; 

The reason why you do not often see such an explanation of a class and an instance, can only an instance? be a global variable, and you don’t very often need global objects if they are not static and / or regular structures of old data.

+2
Apr 25 '09 at 8:00
source share



All Articles