0
What does the "nested scope" code mean? and what is the result?
{ int x = 1; cout << x << endl; { cout << x << endl; int x = 2; cout << x << endl; } cout << x << endl; }
2 odpowiedzi
+ 1
It basically limits the scope of declared variables inside the curly brackets. I believe (correct me if I'm wrong), that the first cout will use the x outside the nested scope, but then a new x is declared inside, which is only available to the nested scope. The output might be:
1
1
2
1
+ 1
Maybe this will help a bit
{
int x = 1;
cout <<x;
{
int y = 2;
cout << x << y;//1,2
}
cout <<y; //error, y does not exist anymore
}