digitalmars.D.learn - Property change problem
- Zarathustra (47/47) Jul 21 2008 I have got following code:
- Jarrett Billingsley (10/11) Jul 21 2008 I really wish the compiler could catch stuff like this. It has no effec...
- Koroskin Denis (3/15) Jul 21 2008 That's why we need C++-style references!
- Zarathustra (2/26) Jul 21 2008 Hmm it's realy strange and in my opinion it harms the properties idea. M...
- Manfred_Nowak (7/8) Jul 22 2008 What is your idea of a property?
I have got following code: ________________________________________________________ module main; import std.stdio; import std.process; struct SSome{ long a = 0; long b = 0; } class CFoo{ private SSome m_u; private SSome m_v; public SSome u(SSome o_u){ return this.m_u = o_u; } public SSome u( ){ return this.m_u; } public SSome v(SSome o_v){ return this.m_v = o_v; } public SSome v( ){ return this.m_v ; } } class CBar{ private CFoo m_foo; public CFoo foo(CFoo o_foo){ return this.m_foo = o_foo; } public CFoo foo( ){ return this.m_foo ; } this() body{ this.m_foo = new CFoo; } } void main(char [][] args){ SSome l_some = SSome(1, 2); CFoo l_foo = new CFoo; writefln("l_foo.u.a: ", l_foo.u.a); l_foo.u = l_some; writefln("l_foo.u.a: ", l_foo.u.a); CBar l_bar = new CBar; writefln("l_bar.foo.u.a: ", l_bar.foo.u.a); l_bar.foo.u.a = 5; // no effect writefln("l_bar.foo.u.a: ", l_bar.foo.u.a); system("pause"); } ________________________________________________________ result: l_foo.u.a: 0 l_foo.u.a: 1 l_bar.foo.u.a: 0 l_bar.foo.u.a: 0 ________________________________________________________ How to easy change value of property l_bar.foo.u.a? Because the above way doesn't work.
Jul 21 2008
"Zarathustra" <adam.chrapkowski gmail.com> wrote in message news:g62dv7$gao$1 digitalmars.com...l_bar.foo.u.a = 5; // no effectI really wish the compiler could catch stuff like this. It has no effect because it's doing something like this: auto temp = l_bar.foo.u; temp.a = 5; And then throwing away 'temp'. That is, it's not modifying l_bar.foo.u, it's modifying a temporary copy of it. It would work if u() returned an SSome* (that is, "return &this.m_u;"), but I don't know if that's acceptable for you.
Jul 21 2008
On Mon, 21 Jul 2008 21:26:04 +0400, Jarrett Billingsley <kb3ctd2 yahoo.com> wrote:"Zarathustra" <adam.chrapkowski gmail.com> wrote in message news:g62dv7$gao$1 digitalmars.com...That's why we need C++-style references!l_bar.foo.u.a = 5; // no effectI really wish the compiler could catch stuff like this. It has no effect because it's doing something like this: auto temp = l_bar.foo.u; temp.a = 5; And then throwing away 'temp'. That is, it's not modifying l_bar.foo.u, it's modifying a temporary copy of it. It would work if u() returned an SSome* (that is, "return &this.m_u;"), but I don't know if that's acceptable for you.
Jul 21 2008
Koroskin Denis Wrote:On Mon, 21 Jul 2008 21:26:04 +0400, Jarrett Billingsley <kb3ctd2 yahoo.com> wrote:Hmm it's realy strange and in my opinion it harms the properties idea. Maybe, one day it will be changed. I hoped that was only my mistake :(. Ok, thanks a lot for help and if anybody know other solution I would like to know it."Zarathustra" <adam.chrapkowski gmail.com> wrote in message news:g62dv7$gao$1 digitalmars.com...That's why we need C++-style references!l_bar.foo.u.a = 5; // no effectI really wish the compiler could catch stuff like this. It has no effect because it's doing something like this: auto temp = l_bar.foo.u; temp.a = 5; And then throwing away 'temp'. That is, it's not modifying l_bar.foo.u, it's modifying a temporary copy of it. It would work if u() returned an SSome* (that is, "return &this.m_u;"), but I don't know if that's acceptable for you.
Jul 21 2008
Zarathustra wrote:it harms the properties ideaWhat is your idea of a property? -manfred -- Maybe some knowledge of some types of disagreeing and their relation can turn out to be useful: http://blog.createdebate.com/2008/04/07/writing-strong-arguments/
Jul 22 2008