Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
December 23, 2005 desired side effect | ||||
---|---|---|---|---|
| ||||
Hi I'm trying to change the value of the locale variable 'var' in main() via a class. Can someone please tell me what I'm doing wrong? output: ======= B.m_val.m_value=test main var=test B.m_val.m_value=test2 main var=test -> can't change this to 'test2'! code: ===== class A { char[] m_value; } class B { A m_val; char[] NewVal( char[] val ) { m_val = new A; m_val.m_value = val; printf("B.m_val.m_value=%s\n", cast(char*)m_val.m_value); return m_val.m_value; } void Set( char[] val ) { m_val.m_value = val; printf("B.m_val.m_value=%s\n", cast(char*)m_val.m_value); } } int main() { auto b = new B; char[] var = b.NewVal("test"); printf("main var=%s\n", cast(char*)var); b.Set("test2"); printf("main var=%s\n", cast(char*)var); return 0; } thx Joris |
December 23, 2005 Re: desired side effect | ||||
---|---|---|---|---|
| ||||
Posted in reply to JP | JP wrote:
[...]
> main var=test -> can't change this to 'test2'!
[...]
By design class B does not know anything about the variables one of its memebers is assigning to.
-manfred
|
December 23, 2005 Re: desired side effect | ||||
---|---|---|---|---|
| ||||
Posted in reply to JP | JP wrote: > class B > { [...] > void Set( char[] val ) > { > m_val.m_value = val; //<<< this line > printf("B.m_val.m_value=%s\n", cast(char*)m_val.m_value); > } > } > > I think the problem is in the marked line. What this line does is to change what m_val.m_value points to. Because m_val.m_value is a different pointer than main.var, changing it doesn't effect the array in main. To change var, you need to change it's contents, and as far as I know there is no way to do that without some sort of reference to main.var. A number of solutions occur to me but which would work depends on what you are doing. Hope this helps. |
December 23, 2005 Re: desired side effect | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote:
> JP wrote:
>
>> class B
>> {
>
> [...]
>
>> void Set( char[] val )
>> {
>> m_val.m_value = val; //<<< this line
>> printf("B.m_val.m_value=%s\n", cast(char*)m_val.m_value);
>> }
>> }
>>
>>
> I think the problem is in the marked line.
>
> What this line does is to change what m_val.m_value points to. Because m_val.m_value is a different pointer than main.var, changing it doesn't effect the array in main. To change var, you need to change it's contents, and as far as I know there is no way to do that without some sort of reference to main.var.
>
> A number of solutions occur to me but which would work depends on what you are doing. Hope this helps.
I */think/* he could pull it off by using slice semantics. I haven't tested it, though.
-- Chris Sauls
|
December 24, 2005 Re: desired side effect | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote: > JP wrote: > >> class B >> { > > [...] > >> void Set( char[] val ) >> { >> m_val.m_value = val; //<<< this line >> printf("B.m_val.m_value=%s\n", cast(char*)m_val.m_value); >> } >> } >> >> > I think the problem is in the marked line. > > What this line does is to change what m_val.m_value points to. Because m_val.m_value is a different pointer than main.var, changing it doesn't effect the array in main. To change var, you need to change it's contents, and as far as I know there is no way to do that without some sort of reference to main.var. > > A number of solutions occur to me but which would work depends on what you are doing. Hope this helps. Thanks for your reply. I was writing a command line parser, but couldn't get a reference to the variables, which would have been neat: auto cmd = new Parser; char[] verbose = cmd.AddOption("verbose", "--verbose", "print more to stdout"); char[] connectstring = cmd.AddArgument("connectstring", "username/password@db"); char[] database = cmd.AddArgument("table", "table to export"); cmd.Parse(args); printf("connectstring=%s\n", cast(char*)connectstring); |
December 27, 2005 Re: desired side effect | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Sauls | Chris Sauls wrote:
> BCS wrote:
>
>> JP wrote:
>>
>>> class B
>>> {
>>
>>
>> [...]
>>
>>> void Set( char[] val )
>>> {
>>> m_val.m_value = val; //<<< this line
>>> printf("B.m_val.m_value=%s\n", cast(char*)m_val.m_value);
>>> }
>>> }
>>>
>>>
>> I think the problem is in the marked line.
>>
>> What this line does is to change what m_val.m_value points to. Because m_val.m_value is a different pointer than main.var, changing it doesn't effect the array in main. To change var, you need to change it's contents, and as far as I know there is no way to do that without some sort of reference to main.var.
>>
>> A number of solutions occur to me but which would work depends on what you are doing. Hope this helps.
>
>
> I */think/* he could pull it off by using slice semantics. I haven't tested it, though.
>
> -- Chris Sauls
yes, maybe but only if he doesn't have to extend the array. Concatenate/extend will sometimes move the data and change your array's ptr.
|
Copyright © 1999-2021 by the D Language Foundation