problem about variable scope
-
int nVar = 0; //GlobalVar int main(int argc, char* argv[]) { int nTest1 = nVar; int nVar = 1; //1stLevelLocalVar nTest1 = nVar; { int nTest2 = ::nVar; // can acess global var, but how to access 1stLevelLocalVar int nVar = 2; //n2ndLevelVar nTest2 = nVar; } return 0; }
-
int nVar = 0; //GlobalVar int main(int argc, char* argv[]) { int nTest1 = nVar; int nVar = 1; //1stLevelLocalVar nTest1 = nVar; { int nTest2 = ::nVar; // can acess global var, but how to access 1stLevelLocalVar int nVar = 2; //n2ndLevelVar nTest2 = nVar; } return 0; }
Here's your chance to learn that global variables are crap. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
-
int nVar = 0; //GlobalVar int main(int argc, char* argv[]) { int nTest1 = nVar; int nVar = 1; //1stLevelLocalVar nTest1 = nVar; { int nTest2 = ::nVar; // can acess global var, but how to access 1stLevelLocalVar int nVar = 2; //n2ndLevelVar nTest2 = nVar; } return 0; }
Don't do it. Never, ever reuse variable names like that. It just leads to all sorts of problems with accidentally using the wrong one.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Don't do it. Never, ever reuse variable names like that. It just leads to all sorts of problems with accidentally using the wrong one.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
i just do it for test purpose
-
int nVar = 0; //GlobalVar int main(int argc, char* argv[]) { int nTest1 = nVar; int nVar = 1; //1stLevelLocalVar nTest1 = nVar; { int nTest2 = ::nVar; // can acess global var, but how to access 1stLevelLocalVar int nVar = 2; //n2ndLevelVar nTest2 = nVar; } return 0; }
Without the global :: specifier i believe the name alone will get the variable at the next outer level of scope. e.g.
int main(int argc, char* argv[])
{
int t0, t1, t2;int v = 0; t0 = v; { t1 = v+1; // = 0 + 1 = 1 int v = t1; { t2 = v+1; // = 1 + 1 = 2 int v = t2; } } return 0;
}
...cmk Save the whales - collect the whole set
-
int nVar = 0; //GlobalVar int main(int argc, char* argv[]) { int nTest1 = nVar; int nVar = 1; //1stLevelLocalVar nTest1 = nVar; { int nTest2 = ::nVar; // can acess global var, but how to access 1stLevelLocalVar int nVar = 2; //n2ndLevelVar nTest2 = nVar; } return 0; }
There is no syntax to say "one scope outward". You can only access the global scope with a plain
::
(And obviously, don't write real code like this, your teammates will thank you) --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD -
int nVar = 0; //GlobalVar int main(int argc, char* argv[]) { int nTest1 = nVar; int nVar = 1; //1stLevelLocalVar nTest1 = nVar; { int nTest2 = ::nVar; // can acess global var, but how to access 1stLevelLocalVar int nVar = 2; //n2ndLevelVar nTest2 = nVar; } return 0; }
Like everyone said, it's a bad idea to do this, in fact, the C# compiler doesn't compile code like yours.. But the answer to your questions is to just use
nVar
. That'll refer to the variable in the current (or enclosing) scope. Regards Senthil _____________________________ My Blog | My Articles | WinMacro