4

I came across one project,where I found some code which I is not understandable to me. I just started with C++, so it seems like a big problem for me. I am providing few lines of my project,which I am not able to 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...

  1. What we can achieve by doing this operation?
  2. What is happening in inside braces of do-while loop?
  3. What is the scope of the initialized variables (I mentioned) inside do-while loop?
  4. Is it C++11?

Help me to understand this.

someone
  • 1,638
  • 3
  • 21
  • 36
  • 2
    No. It is not a new C++11 feature. I don't think it is even a new feature introduced in C with Classes. This is a feature inherited from C. – Mark Garcia Aug 07 '13 at 05:20

4 Answers4

7
  • What we can achieve by doing this operation?

You introduce a scope block for the variables inside.

  • What is happening in inside braces of do-while loop?

See above.

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

The variables go out of scope at the end of the braces, they're there for that sole reason. One use case I can think of for this is a scoped_lock or something similar to it for multi-threaded applications.

  • Is it C++11?

No.

Rapptz
  • 20,807
  • 5
  • 72
  • 86
2

Even in C you can open a brace in any point where a statement is permitted.

This allows to declare and use new variables without interferring with the enclosing scope. For example:

... 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 scope: these two variables are created when entering the block and destroyed when exiting the block (while instead n and data are defined outside). This can help readability by avoiding separation between declaration and use or between declaration and initialization (in old C you were not allowed to put a variable declaration right before use... all locals were required to be declared at start of function: an annoying problem if you don't know yet the value to give them).

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

 {
     Lock mylock(resource);
     use_resource();
 }
6502
  • 112,025
  • 15
  • 165
  • 265
1

Here are your answers:

Ans 1,2. This is used to define a new scope.

Ans 3. The scope of variables end as soon as the control moves out of the block

Ans 4. Probably the coder comes from C background and it is not particular to C++11 as in C, variables can only be declared at the beginning of a new scope.

Please look at THIS and THIS for further reference.

Community
  • 1
  • 1
Saksham
  • 9,037
  • 7
  • 45
  • 73
  • 3
    Why would use of this feature be particular to someone with a C background? It seems to me that it's actually a much more useful feature in C++, because it actually serves a non-aesthetic function. It can allow destructors to be called prematurely. – Benjamin Lindley Aug 07 '13 at 05:29
  • @BenjaminLindley As you might be knowing that C doesn't allow declaration of variables in the middle of the program but only just after the opening braces. So to declare variables in C in the middle of the program, a new scope is introduced. – Saksham Aug 07 '13 at 05:36
  • What Benjamin is saying is that it's more likely the scope block was intentionally used as a C++ construct rather than a C construct. It's common to use scope blocks in C++. One example is when you use RAII mutex locks, and you want to control the lifetime of the lock. – paddy Aug 07 '13 at 05:41
  • @paddy. yes maybe. but this habit comes more from a C programmer :). And I really don't understand why the other answers are upvoted rather than mine – Saksham Aug 07 '13 at 05:46
0

There are five types of scopes in C++

Function
File
Block
Function Prototype
Class

The code which you shared shows 'block scope'

Block scope

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

Saqlain
  • 17,490
  • 4
  • 27
  • 33