In C++, what is the scope resolution ("order of precedence") for shadowed variable names?
- by Emile Cormier
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?