www.digitalmars.com         C & C++   DMDScript  

D - assigning to 'this'

reply "Carlos Santander B." <carlos8294 msn.com> writes:
Why does an assignation to 'this' doesn't work?

class A {
    int a;
    void assign(A tmp) { this=tmp; }   //This line
}
void main() {
    A a1= new A(),a2=new A();
    a1.a=4;
    a2.a=5;
    a1.assign(a2);
    printf('%d %d'\n,a1.a,a2.a);
}

Instead of "5 5", it outputs "4 5". I know you might find this weird, but
I'd like to know the reasoning behind it.

-------------------------
Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.497 / Virus Database: 296 - Release Date: 2003-07-04
Jul 07 2003
parent reply Patrick Down <pat codemoon.com> writes:
"Carlos Santander B." <carlos8294 msn.com> wrote in news:bed76p$1lsp$1
 digitaldaemon.com:

 Why does an assignation to 'this' doesn't work?
 
 class A {
     int a;
     void assign(A tmp) { this=tmp; }   //This line
 }
 void main() {
     A a1= new A(),a2=new A();
     a1.a=4;
     a2.a=5;
     a1.assign(a2);
     printf('%d %d'\n,a1.a,a2.a);
 }
 
 Instead of "5 5", it outputs "4 5". I know you might find this weird, 

 I'd like to know the reasoning behind it.

Try this. void assign(inout A tmp) { this=tmp; } //This line
 
 -------------------------
 Carlos Santander
 
 
 ---
 Outgoing mail is certified Virus Free.
 Checked by AVG anti-virus system (http://www.grisoft.com).
 Version: 6.0.497 / Virus Database: 296 - Release Date: 2003-07-04
 
 
 

Jul 07 2003
parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"Patrick Down" <pat codemoon.com> escribiσ en el mensaje
news:Xns93B1D6772AD29patcodemooncom 63.105.9.61...
| "Carlos Santander B." <carlos8294 msn.com> wrote in news:bed76p$1lsp$1
|  digitaldaemon.com:
|
| > Why does an assignation to 'this' doesn't work?
| >
|
| Try this.
|
|      void assign(inout A tmp) { this=tmp; }   //This line
|

Same result.

—————————————————————————
Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.497 / Virus Database: 296 - Release Date: 2003-07-04
Jul 07 2003
parent reply Patrick Down <pat codemoon.com> writes:
"Carlos Santander B." <carlos8294 msn.com> wrote in news:bedbq5$1qb1$1
 digitaldaemon.com:

 "Patrick Down" <pat codemoon.com> escribiσ en el mensaje
 news:Xns93B1D6772AD29patcodemooncom 63.105.9.61...
| "Carlos Santander B." <carlos8294 msn.com> wrote in news:bed76p$1lsp$1
|  digitaldaemon.com:
|
| > Why does an assignation to 'this' doesn't work?
| >
|
| Try this.
|
|      void assign(inout A tmp) { this=tmp; }   //This line

Actually that was stupid of me. The issue is that you are not changing the object that a1 is pointing to. "this" is like a local parameter to a functionchangeing it only chages it for the scope of the functions.
Jul 07 2003
parent "DeadCow" <deadcow-remove-this free.fr> writes:
"Patrick Down" <pat codemoon.com> a ιcrit dans le message news:
Xns93B1E340F1A6Cpatcodemooncom 63.105.9.61...

 Actually that was stupid of me.  The issue is that you are not
 changing the object that a1 is pointing to.  "this" is like
 a local parameter to a functionchangeing it only chages it
 for the scope of the functions.

"this" must be kind of "read only" and produce error on assignement, like ".size" property of object. -- Nicolas Repiquet
Jul 14 2003