quick question about scope resolution

This is a discussion on quick question about scope resolution within the C++ Programming forums, part of the General Programming Boards category; lets say i have this code... Code: int i; // defined global int main() { int i; { int i; ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    552

    quick question about scope resolution

    lets say i have this code...

    Code:
    int i; // defined global
    int main()
    {
      int i;
      {
        int i;
        {
          int i;
          {
            int i;
            {
              int i;
              ...
      }}}}
       return 0;
    }
    How would I (can I) access, say, the third nested "i" from the innermost scope. And you can spare me the tongue lashings, Im just being curious, I would never do such a thing.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    AFAIK you cannot. Stroustrup's book says it: "There is no way to use a hidden local name"

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    okay
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Oh I dont know

    Code:
    /*Assume VC++6*/
    int i; // defined global
    int main()
    {
      int i = 1;//block 1
      {
        int i = 2;//block 2
        {
          int i = 3;//block 3
          {
            int i = 4;//block 4
            {
              int i = 5;//block 5
              
    		  _asm{
              	push eax
    		lea eax,i
    		add eax, 2 * SIZE i
    		mov eax,dword ptr [EAX]
    		mov i,eax
              	pop eax
              }
    
              std::cout << "Value in block 3 = " << i << std::endl;
      }}}}
       return 0;
    }
    Just kidding though....you wouldnt want to do this

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Ah, finally I've got a reason to learn asm.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    How would I (can I) access, say, the third nested "i" from the innermost scope. And you can spare me the tongue lashings, Im just being curious, I would never do such a thing.
    Namespaces could solve the problem, its not nice but possible.

    Code:
    #include <iostream>
    
    namespace foo
    {
    	int i = 5;
    	namespace foo2
    	{
    		int i = 10;
    		namespace foo3
    		{
    			int i = 20;
    		}
    	}
    }
    
    int main()
    {
    	std::cout << "First i is " << foo::i << std::endl;
    	std::cout << "Second i is " << foo::foo2::i << std::endl;
    	std::cout << "Third i is " << foo::foo2::foo3::i << std::endl;
    	return 0;
    }
    But still I prefer Fordy´s approach .

  7. #7
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I like ripper079's approach, I didn't thik of namespace this way.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Oooh, I wanna learn ASM Is it as confusing and mind-boggling yet rewarding (in terms of impressing friends) as it looks?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Yep.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. quick question (C/C++)
    By owi_just in forum C Programming
    Replies: 2
    Last Post: 03-19-2005, 08:44 AM
  3. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  4. Quick Question on File Names and Directories
    By Kyoto Oshiro in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2002, 01:54 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21