Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++, what is the scope resolution ("order of precedence") for shadowed variable names?

In C++, what is the scope resolution ("order of precedence") for shadowed variable names? I can't seem to find a concise answer online.

For example:

#include <iostream>

int shadowed = 1;

struct Foo
{
    Foo() : shadowed(2) {}

    void bar(int shadowed = 3)
    {
        std::cout << shadowed << std::endl;
            // What does this output?

        {
            int shadowed = 4;
            std::cout << shadowed << std::endl;
                // What does this output?
        }
    }

    int shadowed;
};


int main()
{
    Foo().bar();
}

I can't think of any other scopes where a variable might conflict. Please let me know if I missed one.

What is the order of priority for all four shadow variables when inside the bar member function?

like image 557
Emile Cormier Avatar asked May 10 '10 17:05

Emile Cormier


People also ask

What is scope resolution in C?

The scope resolution operator is used to reference the global variable or member function that is out of scope. Therefore, we use the scope resolution operator to access the hidden variable or function of a program.

What is scope resolution operator in C with example?

The scope resolution operator ( :: ) is used for several reasons. For example: If the global variable name is same as local variable name, the scope resolution operator will be used to call the global variable. It is also used to define a function outside the class and used to access the static variables of class.

What is scope resolution operator :: in C ++?

The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class.

Which operator can be used to resolve the scope of a variable?

In C++, scope resolution operator is ::. Scope resolution operator in C++ can be used for: Accessing a global variable when there is a local variable with same name. Defining a function outside a class.


1 Answers

Your first example outputs 3. Your second outputs 4.

The general rule of thumb is that lookup proceeds from the "most local" to the "least local" variable. Therefore, precedence here is block -> local -> class -> global.

You can also access each most versions of the shadowed variable explicitly:

// See http://ideone.com/p8Ud5n
#include <iostream>

int shadowed = 1;

struct Foo
{
    int shadowed;
    Foo() : shadowed(2) {}
    void bar(int shadowed = 3);
};

void Foo::bar(int shadowed)
{
    std::cout << ::shadowed << std::endl; //Prints 1
    std::cout << this->shadowed << std::endl; //Prints 2
    std::cout << shadowed << std::endl; //Prints 3
    {
        int shadowed = 4;
        std::cout << ::shadowed << std::endl; //Prints 1
        std::cout << this->shadowed << std::endl; //Prints 2
        //It is not possible to print the argument version of shadowed
        //here.
        std::cout << shadowed << std::endl; //Prints 4
    }
}

int main()
{
    Foo().bar();
}
like image 81
Billy ONeal Avatar answered Sep 22 '22 19:09

Billy ONeal