Semicolon after class declaration braces
The semi-colon after the closing brace in a type declaration is required by the language. It's been that way since the earliest versions of C.
And yes, people do indeed do the declaration you just put up there. It's useful for creating scoped types inside of methods.
void Example() {
struct { int x; } s1;
s1.x = 42;
struct ADifferentType { int x; };
}
In this case, I think it's clear why the semi-colons are needed. As to why it's needed in the more general case of declaring in the header file I'm unsure. My guess is that it's historical and was done to make writing the compiler easier.
Semicolon after class declaration
Explain, please, why is it possible to use semicolon here and what does ';' means in these context
Semicolons are optional in ruby. This means that using them is actually valid syntax. Having a newline is enough for the parser to separate statements. In the case you mention the semicolon is required as there is not a newline as a statement separator.
The following are all valid options:
class Dog
end
class Dog;
end
class Dog; end
The following is invalid (with ruby >= 2.3 this seems to be fine though)
class Dog end
For class or method definitions with an empty body using one line with a semicolon separator is idiomatic in ruby. If you have a body you'll usually omit the semicolon and place the content on a new line.
UPD: I thought, that
Java's syntax parser doesn't allow as much flexibility as ruby's parser. A class or method definition in Java is strict syntax which requires a newline after the class name and before the definition body.
When is a semicolon after } mandated in c/c++?
int a[2] = {1,2}, j = 5;
When initialization of array or structure are done with {}
all the subsequent variables are declared after ,
.
Edit: As you changed your question; ;
is mandatory after the class
, enum
, initialization syntax declarations.
class A {}; // same for `struct
enum E {}; // enum class (c++0x)
int a[] = {1,2}; // array or object initialization
And as per comment by @rubenvb:
do {} while(condition);
C++ Semicolon after class, struct, enum,... useless but compilers still emit errors?
It's a leftover from C. You can declare variables after cladd/struct/enum declaration. the code:
class A{};
A a;
Is identical to:
class A{} a;
The compiler needs the ;
to know you don't want to declare variables after the class declaration.
When should I use a semicolon after curly braces?
You use a semicolon after a statement. This is a statement:
var foo = function() { alert("bar");};
Class declaration confusion - name between closing brace and semi-colon
rect
is the name of a variable (an object in this case).
It is exactly as if it had said:
int rect;
except instead of int
there is a definition of a new type, called a CRectangle
. Usually, class types are declared separately and then used as
CRectangle rect;
as you are probably familiar with, but it's perfectly legal to declare a new type as part of a declaration like that.
And yes, it works for structs:
struct SRectangle { int x, y; } rect;
In fact you don't even have to give the type a name if you don't plan to use it again:
struct { int x, y; } rect;
that's called an "anonymous struct" (and it also works for classes).
When do we need to put semicolon after curly brackets? i.e.: };
"Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once."
Java - Anonymous Classes
Instead of doing something like this:
class MyThread extends Thread {
@Override
public void run(){
// something
}
}
...
Thread myThread= new MyThread();
You can sorten your code by doing it this other way using anonymous classes.
Thread myThread=new Thread(){
@Override
public void run(){
// something
}
};
Auto insert semicolon after class definition in Codelite
CodeLite does not do that by default and there is no setting that you can modify to do that.
You can however, achieve this in two other ways:
Use the class wizard:
Create your class using the 'Class Wizard': Right click on a folder in the tree view and select New class
Use the abbreviations plugin
- From the menu bar:
plugins->abbreviations->settings
- Click on the new entry button (green plus icon)
Name it "class" (or a name of your choice) and paste the following code:
class | {
public:};
Click "Save"
- Now when typing in the editor
class
hit:Ctrl-ENTER
and you will see this entry - select it and it will added to the text editor for you.
NOTE: the |
marker indicates where CodeLite will place the caret, You can have multiple carets. So the above example can be expanded to something like this:
class | {
public:
|(){}
~|(){}
};
Now, when CodeLite adds this snippet to the editor, you will get 3 carets. Typing the class name will add them in all three locations
NOTE 2: The above was tested using CodeLite 11.0.8 (git latest), but it should work with CodeLite 11.0 as well
HTH,
Eran, Author of CodeLite IDE
Related Topics
Why Can't I Open Avi Video in Opencv
Variadic Template in VS 2012 (Visual C++ November 2012 Ctp)
What Is the Proper Function for Comparing Two C-Style Strings
What Exactly Is the Difference Between "Pass by Reference" in C and in C++
Copy a Streambuf's Contents to a String
Constexpr Const VS Constexpr Variables
Why Does C++11 Have 'Make_Shared' But Not 'Make_Unique'
Why Do Some Experienced Programmers Write Comparisons with the Value Before the Variable
Copy Elision: Move Constructor Not Called When Using Ternary Expression in Return Statement
C++ Function Callbacks: Cannot Convert from a Member Function to a Function Signature
What Is Std::Decay and When It Should Be Used
Which Stl Container Should I Use for a Fifo
Why Can a T* Be Passed in Register, But a Unique_Ptr<T> Cannot
Use Std::Fill to Populate Vector with Increasing Numbers
Difference Between Using #Include<Filename> and #Include<Filename.H> in C++