Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
May 01, 2004 access to outer scope variables | ||||
---|---|---|---|---|
| ||||
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 Re: access to outer scope variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | 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 Re: access to outer scope variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | 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 Re: access to outer scope variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | "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 Re: access to outer scope variables | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | 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?! ;-)
|
Copyright © 1999-2021 by the D Language Foundation