Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem about variable scope

problem about variable scope

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HeartFriend
    wrote on last edited by
    #1

    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; }

    C R C M S 5 Replies Last reply
    0
    • H HeartFriend

      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; }

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • H HeartFriend

        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; }

        R Offline
        R Offline
        Ryan Binns
        wrote on last edited by
        #3

        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"

        H 1 Reply Last reply
        0
        • R Ryan Binns

          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"

          H Offline
          H Offline
          HeartFriend
          wrote on last edited by
          #4

          i just do it for test purpose

          1 Reply Last reply
          0
          • H HeartFriend

            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; }

            C Offline
            C Offline
            cmk
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • H HeartFriend

              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; }

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • H HeartFriend

                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; }

                S Offline
                S Offline
                S Senthil Kumar
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups