mman
mman

Reputation: 89

How to access function scope variables in c++?

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?
    }
}

Upvotes: 0

Views: 178

Answers (1)

R Sahu
R Sahu

Reputation: 206717

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.

Upvotes: 3

Related Questions