summary
stay C/C++ Curly brackets indicate the scope of the variable ,
Local variables declared in braces begin with the scope arguments declaration , End after braces .
{ } It's a “ block ”, alone { } There is no change in the execution order , It's still sequential ,
example 1
void MyProcess(MyType input, MyType &output) { MyType filter = input; {
MyType temp; step1(filter,temp); } { MyType temp;
step2(filter,temp); } { MyType temp; step3(filter,temp); }
output = filter; }
The above program implements a simple pipeline / Filter structure :
temp1 temp2 temp3
↓ ↓ ↓
input --> step1 --> step2 --> step3 --> output
temp They're all temporary variables ,
If there is no brace constraint , Each temporary variable exists in the function scope , Then the probability of error increases greatly when the process is increased or decreased frequently .
In braces , Not only is the program clear to read , And it's not easy to make mistakes
example 2
{ int a=0; { int b=0; a=1; // correct , still a In the scope of } b=1; // error , Because I'm not here b Scope of ,b It's been destroyed }
Technology