Thread overview
access to outer scope variables
May 01, 2004
J Anderson
May 04, 2004
Stewart Gordon
May 04, 2004
J Anderson
Mar 17, 2005
Walter
Mar 17, 2005
John Reimer
May 01, 2004
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 04, 2004
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
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/
March 17, 2005
"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>.


March 17, 2005
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?! ;-)