If you writing a class and you use something like this
...
int x;
{ x = 2; }
...
This would be an initialisation block right?
So how would you use curly braces just to designate scope, so that they are executed like any other code? Basically so they are not part of the constructor.
Ex
if you were to use a block inside a switch statement, would this be executed like an initialisation block?
switch(...)
{
case :
{ // this right here how does the compiler know the difference?
...
break;
}
}
Initialization blocks only apply to blocks that are in the scope of a class and are not preceded by a function prototype (i.e. blocks that are not the body of a function).
Within the body of a function, any blocks that you create will introduce scope but will not be interpreted as an initialization.
Example: