Could not downgrade this code ... Is it C ++ 11?

I came across a project where I found code that I don’t understand. I was just starting out with C++ , so for me this seems like a big problem. I provide a few lines of my project that I cannot understand.

 class abc { public: //some stuff abc(); }; abc::abc() { int someflag = 0; //code if(someflag == 0) { do { //few strcpy operations { //(My Question)Without any condition braces started //variable initialization } }while(condition); } } 

Now my questions ...

  • What can we achieve by performing this operation?
  • What happens in the inside braces of a do-while ?
  • What is the scope of the initialized variables (I mentioned) inside the do-while loop?
  • Is this C ++ 11?

Help me figure this out.

+4
source share
4 answers
  • What can we achieve by performing this operation?

You enter a visibility block for variables inside.

  • What happens in the inside braces of a do-while loop?

See above.

  • What is the scope of the initialized variables (I mentioned) inside the do-while loop?

Variables go out of scope at the end of curly brackets; they are for this only reason. One use case that I can think of is scoped_lock or something similar to it for multi-threaded applications.

  • Is this C ++ 11?

Not.

+7
source

Even in C, you can open the bracket at any point where approval is allowed.

This allows you to declare and use new variables without interfering with the scope. For instance:

 ... code before ... { int i = 0, sum = 0; while (i < n) { sum += dothis(data[i++]); } dothat(sum); } ... code after ... 

the two variables i and sum have nothing to do with variables with the same name in the enclosing area: these two variables are created when the block is entered and destroyed when the block is exited (instead, n and data defined outside). This can help readability by avoiding the distinction between declaration and use or between declaration and initialization (in old C, you were not allowed to enter a variable declaration immediately before use ... all locals should have been declared at the beginning of the function: annoying problem if you haven't already know the meanings to give them).

If you are in C ++ and these block-local variables are of class type, the constructor is called when a block is entered (and not when a function is entered) and is immediately destroyed when the block exits. This can be very useful, for example, for locks.

  { Lock mylock(resource); use_resource(); } 
+2
source

Here are your answers:

Ans 1,2. Used to define a new area.

Ans 3. The scope of the variables ends as soon as the control exits the block

Ans 4. Probably, the encoder comes from the C-background and does not apply to C ++ 11, as in C, variables can only be declared at the beginning of a new area.

See IT and IT > for further references.

+1
source

In C ++, there are five types of scope

 Function File Block Function Prototype Class 

In the code you shared, the "block area" is displayed

Block area

Blocks are parts of C ++ code that are enclosed in braces ({....}). Identifiers declared in a block have a block area and are visible from their definition points to the end of the innermost containing block. The name of the duplicate identifier in the block hides the value of the identifier with the same name that is defined outside the block. The variable name declared in the block is local to this block. It can only be used in it and other blocks contained under it.

0
source

Source: https://habr.com/ru/post/1495673/


All Articles