zaterdag 16 juni 2007

Blocks and variables

When you have multiple blocks defined in your code, the outer variables will be visible in the inner blocks, but variables defined in a inner block are not visible in outer blocks.

Example:

int main()
{
int x = 20;
{
int y = 100;
cout << x << endl; // valid
cout << y << endl; // valid
}
cout << y << endl; // INVALID compilation error, y is not defined here!
}

Geen opmerkingen: