www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - desired side effect

reply JP <Joris.Putcuyps skynet.be> writes:
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
next sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
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
prev sibling parent reply BCS <BCS_member pathlink.com> writes:
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);
     }
 }
 
 

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
next sibling parent reply Chris Sauls <ibisbasenji gmail.com> writes:
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);
     }
 }

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
Dec 23 2005
parent BCS <BCS_member pathlink.com> writes:
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);
     }
 }

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.
Dec 27 2005
prev sibling parent JP <Joris.Putcuyps skynet.be> writes:
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);
     }
 }

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);
Dec 24 2005