Namespaces
Variants
Views
Actions

Variable Scope

From cppreference.com


A scope is a region of code that shares the same name table. When a variable is declared, it becomes available as a named item from the point immediately after declaration until the end of the scope in which its declared. Two variables cannot have the same name within the same scope. Scopes exist within a larger outer scope, and they inherit all names from their parent scope.

The following example code illustrates the basics of scopes.

void scope_example() {
    int x = 3; //x1
    //int x = 4; //error: x is already declared
    {
        int x = 5; //x2 -- valid -- the {} create a new scope inside the scope of scope_example().
                   //x in this scope will refer to the x with the value of 5 ('x2').
                   //This is referred to as name hiding, and it should be avoided.
    }
    //x now referrs to 'x1' again
}
void scope_example2() {
    int x = 5; //this x and scope_example()'s x are two unrelated variables
}

Contents

[edit] Block Scope

A block is delineated by an opening { and a closing }. Variables declared in a block are accessible from immediately after the declaration until the end of the block. Any sub-blocks declared within a block will have access to any variables declared in all scopes above it that are declared before the sub-block opens.

void do_something() {
 
    //top level block -- no variables yet declared
 
    int top;
 
    //currently available: top
 
    {
        //currently available: top
        int nested;
        //currently available: top and nested
    }
 
    //currently available: top -- nested no longer exists
 
    {
        //currently available: top
        int nested;
        //currently available: top and nested
        {
            //currently available: top and nested
            int double_nested;
            //currently available: top, nested, and double_nested
        }
        //currently available: top, nested
        int nested2;
        //currently available: top, nested, and nested2
    }
 
    top = 5; //comparing against the value of top if it were unitialized
             //would be undefined behavior.
 
    if (top == 5) {
        //currently available: top
        int if_block;
        //currently available: top and if_block
    }
 
    //top level block -- top is available
 
}

[edit] Function Scope

Only labels have function scope. They are available within an entire function, even before they are declared and regardless of if they're declared in a sub-block.

void label_example() {
    //SomeLabel is available here
    if (...) {
        for (...) {
            SomeLabel:
            /* Code here */
        }
    }
    //SomeLabel is available here
    if (...) {
        //SomeLabel is available here
    }
}

Note that even though SomeLabel is declared in a deeply nested block, it's available throughout the entire function.

[edit] Namespace Scope

[edit] Class Scope

[edit] Enumeration Scope

[edit] Template Parameter Scope

[edit] Name hiding

It's possible to declare a variable in an inner scope with the same name as a variable in its parent scope. In such a case, the variable of the inner scope shadows the variable of the parent scope. That is, a second variable is created and all uses of the variable within the inner scope refer to that newly created variable. It's no longer possible to refer to the variable of the parent scope.

[edit] friend