Reputation: 93
In C++, I have a situation where I want to access a variable defined in the local scope of the main function from an inner block scope. Here’s a simplified version of my code:
#include <iostream>
using namespace std;
int var = 10; // Global variable
int main(int argc, char *argv[])
{
int var = 20; // Local variable in main
{
int var = 40; // Local variable in inner block
cout << ::var; // Prints 10 (the global variable)
// But I want to print the `var` in the `main` scope (20)
}
return 0;
}
I want cout to print 20, which is the value of var in the scope of main. However, using ::var only gives me the global variable value 10.
Is there a way in C++ to access the local variable in main (value 20) from within this inner block, bypassing the local variable defined in the inner block (value 40)?
I know that C++ doesn’t have an equivalent of Python’s nonlocal keyword, but is there a workaround or technique that allows accessing a variable from an enclosing scope without changing the variable names?
Upvotes: 1
Views: 1842
Reputation: 145194
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. :)
Upvotes: 0
Reputation: 10430
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.
Upvotes: 2
Reputation: 3526
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;
}
Upvotes: 0
Reputation: 206557
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.
Upvotes: 2