digitalmars.D.learn - desired side effect
- JP (43/43) Dec 23 2005 Hi
- Manfred Nowak (6/7) Dec 23 2005 [...]
- BCS (10/20) Dec 23 2005 I think the problem is in the marked line.
- Chris Sauls (3/28) Dec 23 2005 I */think/* he could pull it off by using slice semantics. I haven't te...
- BCS (3/37) Dec 27 2005 yes, maybe but only if he doesn't have to extend the array.
- JP (12/37) Dec 24 2005 Thanks for your reply.
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
Dec 23 2005
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
Dec 23 2005
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.
Dec 23 2005
BCS wrote:JP wrote:I */think/* he could pull it off by using slice semantics. I haven't tested it, though. -- Chris Saulsclass 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.
Dec 23 2005
Chris Sauls wrote:BCS wrote: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.JP wrote:I */think/* he could pull it off by using slice semantics. I haven't tested it, though. -- Chris Saulsclass 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.
Dec 27 2005
BCS wrote:JP wrote: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);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.
Dec 24 2005