www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - access to outer scope variables

reply J Anderson <REMOVEanderson badmama.com.au> writes:
class B
{
   void load()
   {
       printf("val = %d\n", val);
       a.add();
   }
       struct A
   {        void add()
       {
           printf("val = %d\n", val); //Which val is this referring too?
       }
   }

   A a;
     int val;
}


int main (char[][] args)
{
   B b = new B;
     b.load();
}


Output:
val = 0
val = 8719856

What is the second val referring to.  If val is changed to static then 
things will work fine (for one class instance of course).  Shouldn't 
this code be either:
a) Disallowed at compile time
or
b) Have val refer to it's owner object's val (if that's possible).

-- 
-Anderson: http://badmama.com.au/~anderson/
May 01 2004
next sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
J Anderson wrote:

 class B
 {
   void load()
   {
       printf("val = %d\n", val);
       a.add();
   }
       struct A
   {        void add()
       {
           printf("val = %d\n", val); //Which val is this referring too?
The only one that's declared in your code.
       }
   }
 
   A a;
     int val;
 }
 
 
 int main (char[][] args)
 {
   B b = new B;
     b.load();
 }
 
 
 Output:
 val = 0
 val = 8719856
<snip> That's very strange. Are you sure the code is all there? Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
May 04 2004
parent J Anderson <REMOVEanderson badmama.com.au> writes:
Stewart Gordon wrote:

 J Anderson wrote:

 class B
 {
   void load()
   {
       printf("val = %d\n", val);
       a.add();
   }
       struct A
   {        void add()
       {
           printf("val = %d\n", val); //Which val is this referring too?
The only one that's declared in your code.
       }
   }

   A a;
     int val;
 }


 int main (char[][] args)
 {
   B b = new B;
     b.load();
 }


 Output:
 val = 0
 val = 8719856
<snip> That's very strange. Are you sure the code is all there? Stewart.
Yeah I was confused to. Just run it and see. -- -Anderson: http://badmama.com.au/~anderson/
May 04 2004
prev sibling parent reply "Walter" <newshound digitalmars.com> writes:
"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:c70jk9$off$1 digitaldaemon.com...
 What is the second val referring to.  If val is changed to static then
 things will work fine (for one class instance of course).  Shouldn't
 this code be either:
 a) Disallowed at compile time
 or
Correct. I'll fix it.
 b) Have val refer to it's owner object's val (if that's possible).
That's Java <g>.
Mar 17 2005
parent John Reimer <brk_6502 yahoo.com> writes:
Walter wrote:
 "J Anderson" <REMOVEanderson badmama.com.au> wrote in message
 news:c70jk9$off$1 digitaldaemon.com...
 
What is the second val referring to.  If val is changed to static then
things will work fine (for one class instance of course).  Shouldn't
this code be either:
a) Disallowed at compile time
or
Correct. I'll fix it.
b) Have val refer to it's owner object's val (if that's possible).
That's Java <g>.
Woah! That's scary, Walter. Are you /that/ far behind?! ;-)
Mar 17 2005