0

Global scoped variables can be accessed within a function using the :: operator. Since global scopes dont have a name, the left of :: could be empty. How will I access a variable defined in a function scope which is later overridden in a block within the function itself. In the following code, how will i access the variable initialised to 1 ?

extern int reused = 0;
int main()
{
    int reused = 1;
    {
        int reused = 2;
        cout << reused << endl; // how to get the reused inited to 1 here?
    }
}
mman
  • 89
  • 3

1 Answers1

3

You can access the global reused anywhere inside main by using ::reused. However, there is no way to use the second reused inside the block where the third reused is declared. The language does not provide a mechanism for that.

R Sahu
  • 204,454
  • 14
  • 159
  • 270