问题
I want to access a local variable of main function in another scope.
My goal is to print 20 in cout
.
How can I do this?
How is it possible in C++?
int var = 10;
int main(int argc, char *argv[])
{
int var = 20; // this var
{
int var = 40;
cout << ::var; // I want to print `var` variable in main scope.
// But this command print global variable.
}
return 0;
}
回答1:
There is no way to accomplish that. The language does not provide a way to differentiate between the first var
in main
from the second var
.
If you ever write production code, please refrain from using such variables. It will lead to buggy code. You will be confused about which variable is in scope in a given line of code.
回答2:
You can't access a variable (in some cases extern
can be used but not in your code) outside its scope. So, you can't use variable var
(declared inside the innermost block) outside its scope. For example,
int var = 10;
int main(int argc, char *argv[])
{
int var = 20; // this var
{
int var = 40;
}
cout << var; // this will access var declared first and prints 20
return 0;
}
And trust me, there is no need to declare and use variables in this way. It will lead to buggy code. Remember, if you use GCC or clang, always use -Wshadow
compiler flag while compiling as it shows warning if something like this is done anywhere in the code. Try to include it in your IDE or makefile if you don't compile your code from terminal.
回答3:
CASE A
int var = 10;
int main(int argc, char *argv[])
{
int var = 20; // this var
{
int var = 40;
cout << ::var; // This will output 10 because you have used scope of operator that'll point to global variable in C++.
}
return 0;
}
CASE B
int var = 10;
int main(int argc, char *argv[])
{
int var = 20; // this var
{
int var = 40;
cout <<var; // This will output 40 because you are using local variable.
}
return 0;
}
CASE C
int var = 10;
int main(int argc, char *argv[])
{
int var = 20; // this var
{
int var = 40;
}
cout << var; // this will output 20 because using var that is declared in main() function.
return 0;
}
回答4:
Regarding your example code
int var = 10;
int main(int argc, char *argv[])
{
int var = 20; // this var
{
int var = 40;
cout << ::var; // I want to print `var` variable in main scope.
// But this command print global variable.
}
return 0;
}
… and the stated goal in the comment,
” I want to print
var
variable in main scope
… you can do that as follows, simply adding an alias for it:
int var = 10;
int main()
{
int var = 20; // this var
{
auto& outer_var = var;
int var = 40;
cout << outer_var;
}
}
C++ does not provide a scope resolution mechanism that can do this for you. If there were such a mechanism, e.g. relative scope resolution or function names as pseudo namespace names, it would presumably be used, and one would then see at least some code that would be difficult to understand because of use of the same name for different things within a short stretch of code. An alternative language design is like C#, where shadowing (like the inner var
shadows the outer var
) is simply forbidden.
Similarly, C++ does not provide any way to provide additional names for the global namespace. Other namespaces can be aliased, but not the global namespace, so that one can be quite sure that the global namespace isn't referenced via something looking like companyname::counter
, say.
C++ also has a restriction on which operators can be overloaded by namespace scope functions, again to provide a measure of sanity, something one can absolutely rely on. Regarding this last rationale I know that from Bjarne Stroustrup (the language creator). Regarding the rationale for lack of relative scope resolution and lack of global namespace aliasing it's only a qualified guess that it's the same, but it stands to reason. :)
来源:https://stackoverflow.com/questions/38289456/how-can-i-use-local-variable-in-another-scope-in-c