opAssign for most struct assignment calls not executed

Timoses timosesu at gmail.com
Thu Nov 23 15:26:03 UTC 2017


What am I missing?

import std.stdio;

struct A
{
     int value;

     A opAssign(A a)
     {
         writeln(" Assigning");
         return this;             // I know this makes little 
sense, just for illustration
     }
}

class B
{
     A member;

     this(A a)
     {
         this.member = a; // specifically aiming towards this 
assignment...
     }
}

void main()
{
     A a = A(1);
     A aa = A(2);

     A aaa = a;

     auto clas = new B(a);

     writeln("Only this works:");
     aaa = a;
     writeln("or");
     A aaaa;
     aaaa = aa;
}


Output:
Only this works:
  Assigning
or
  Assigning


Note that before "Only this works:" nothing was written.

How can I overwrite the opAssign operator??
I'm sure I'm missing something...


More information about the Digitalmars-d-learn mailing list