Simple block scope
suggest changeThe scope of a variable in a block { ... } begins after its declaration and ends at the end of the block. If there is a nested block, the inner block can hide a variable declared in the outer block.
{
int x = 100;
// ^
// Scope of `x` begins here
//
} // <- Scope of `x` ends here
If a nested block starts within an outer block, a variable declared in the inner block with the same name hides the one in the outer block.
{
int x = 100;
{
int x = 200;
std::cout << x; // <- Output is 200
}
std::cout << x; // <- Output is 100
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents